Reputation:
I need some help calling the AlphaVantage stock market API. For the most part, it works nicely but when it comes to requesting data from stocks that have an ^
symbol in their ticker, something goes wrong.
I get the API response with this code:
var url = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=^vvix&apikey=<KEY>",
response = UrlFetchApp.fetch(url),
json = JSON.parse(response);
Logger.log(json);
and get this error:
Invalid argument: https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&
The ticker symbol of the requested stock is ^vvix
. If I replace the symbol with like vix
(another valid ticker) the code works. Thus, I assume the error has something to do with the ^
sign, but the ^
sign is part of the ticker symbol.
If i use
=IMPORTDATA("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=^vvix&apikey=K9D4PO855OG03KNM")
as a Google Sheets formula, the ^
is not a problem and the data is retrieved
Where is the difference?
Is ^
an invalid character in a JavaScript string?
Any idea whats the problem?
Upvotes: 0
Views: 1717
Reputation: 4034
The caret ("^") is NOT a valid URL character and must be percent encoded if you follow the recommendations of the IETF. This is one of the "unsafe" characters defined in RFC1738
Use %5E
in place of ^
.
More generally, wrap the symbol with:
encodeURI(symbol)
Upvotes: 1