Thursday, May 9, 2024
175
rated 0 times [  180] [ 5]  / answers: 1 / hits: 23617  / 6 Years ago, mon, july 2, 2018, 12:00:00

My api call requires me to pass the api key in the headers, but I'm getting error back from the api service {error:2424452,message:Invalid Api Key}



I know my api key is valid as I can make the same api call in Python just fine, example:



req = requests.Session()
req.headers.update({'x-api-key': 'my-api-key', 'X-Product': 'my-product-name'})
req.get(url)


But in javscript, the same call errors out. I believe I'm not setting the headers correctly or something?



var req = new XMLHttpRequest();
req.onreadystatechange=handleStateChange;
req.open(GET, url, true);
req.setRequestHeader(Host, api.domain.com, x-api-key, my-api-key, X-Product, my-product-name);
req.send();



  • This XMLHttpRequest is not a browser call, rather in an application that support XMLHttpRequest.


More From » xmlhttprequest

 Answers
17

setRequestHeader sets one header and takes two arguments (the name and the value).



If you want to set multiple headers, then call setRequestHeader multiple times. Don't add extra arguments to the first call.


[#54077] Wednesday, June 27, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lexusg

Total Points: 718
Total Questions: 106
Total Answers: 104

Location: Palestine
Member since Tue, Jul 20, 2021
3 Years ago
;