Hi 🙂 I am tying to call back tide station data to populate a survey form using Admiralty API. I used to use csv files for the approprate stations / day / tide data but I have to update these every year manually. Yawn!
I have not come accross request headers using JavaScript before and I am struggling with the syntax. what I have so far is...
function runTideCalcs(){
var xmlhttp = new XMLHttpRequest();
var url = `https://admiraltyapi.azure-api.net/uktidalapi/api/V1/Stations/0065/TidalEvents?duration=1`;
xmlhttp.open("GET",url,false);
xmlhttp.send();
if (xmlhttp.status!=200){
return null;
} else {
var responseJSON=JSON.parse(xmlhttp.responseText)
if (responseJSON.error){
return responseJSON.error;
} else {
if (responseJSON){
return JSON.stringify(responseJSON);
}
else {
return "";
}
}
}
}
....But I am not sure what syntax or where to incorporate the
setRequestHeader("Ocp-Apim-Subscription-Key","{subscription key}");
Naturally I have an Admiralty account and Sub. key. Any pointers would be very welcome.
Thanks
Chris
Solved! Go to Solution.
Hello @ChrisMaclaurin,
I would suggest using an API debugging software like Postman for example, Postman has a nice GUI that help set up the API call to get it in a state where it is working. When it's up and running in Postman there is a nice code snippet functionality that will format the request in a JavaScript XHR format that you can copy over to your JavaScript function and modify as needed.
Hi @ZacharySutherby,
Thanks for the awsome tip! I was so almost there, but using Postman was an excellent suggestion - a few other tweeks and I was there. Postman will certainly come in useful again in future for other projects too, so 1000 thank yous for the signpost. v. much appreciated.
Chris
Hello @ChrisMaclaurin,
I would suggest using an API debugging software like Postman for example, Postman has a nice GUI that help set up the API call to get it in a state where it is working. When it's up and running in Postman there is a nice code snippet functionality that will format the request in a JavaScript XHR format that you can copy over to your JavaScript function and modify as needed.
Hi @ZacharySutherby,
Thanks for the awsome tip! I was so almost there, but using Postman was an excellent suggestion - a few other tweeks and I was there. Postman will certainly come in useful again in future for other projects too, so 1000 thank yous for the signpost. v. much appreciated.
Chris