Hi, From a Survey123 Javascript, I would like to call an API with the HTTP basic authentication method. This is the requirement from the API supplier:
Authentication
Each request requires authentication, which is performed using the HTTP basic authentication method. The header contains the authentication data in the form "Basic username:password" where the phrase "username:password" is Base64 encoded. Username and password are set individually for each external system.
I have tried with:
var url = `<MyUser>:<myPassword>@branchekataloget.dk/api-gateway/Branchekataloget/products?${parameters}`
xmlhttp.open("GET",url,false);
xmlhttp.send();
But get the answer:
Error in <MyScript>.js : 28:1 Expected token `;'
Hope you can point me in the right direction 🙂
Solved! Go to Solution.
Hi, @MartinBrandi - How were you able to do the base64 encoding for the username and password in Survey123 Connect? I have a very similar system to what you're using but the bota() and atob() methods don't seem to work in Survey123. Any suggestions?
@MartinBrandi - Not sure if you meant to mark my response as the solution to this since it was a question. Any insight you can provide would be helpful.
@MartinBrandi - Are you doing that intentionally? My response is not a solution to your question. 😕
Are you a bot, @MartinBrandi?
Seems like @MartinBrandi is just a bot that auto-accepts all responses as solutions and gives them kudos.
Thanks for the expert badge, @MartinBrandi. 🙄
Hello @MartinBrandi,
The expected token error likely indicates that a semicolon is missing from line 28 of the JavaScript function. If there is a semicolon there and the error seems like a red herring I would suggest using a tool called Postman to set up the http request. Once it's working in Postman there is a neat tool that lets you copy a code snippet of the request in JavaScript XHR format.
Hi Zach
Thank you very much for your help, Postman was a great help
This code works. I hope others can benefit from this:
function Myfunction() {
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open("GET", "My URL",null);
xhr.setRequestHeader("Authorization", "Basic <username:password as Base64 encoded> ");
xhr.setRequestHeader("Cookie", "PHPSESSID=ba4jnakh2u0r5l0mgpsndgggul");
xhr.send();
var responseJSON=JSON.parse(xhr.responseText)
return JSON.stringify(responseJSON);
}
Hi, @MartinBrandi - How were you able to do the base64 encoding for the username and password in Survey123 Connect? I have a very similar system to what you're using but the bota() and atob() methods don't seem to work in Survey123. Any suggestions?
@MartinBrandi - Not sure if you meant to mark my response as the solution to this since it was a question. Any insight you can provide would be helpful.
@MartinBrandi - Are you doing that intentionally? My response is not a solution to your question. 😕
Are you a bot, @MartinBrandi?
Seems like @MartinBrandi is just a bot that auto-accepts all responses as solutions and gives them kudos.
Thanks for the expert badge, @MartinBrandi. 🙄