HTTP basic authentication method in Survey123 javascript

1226
11
Jump to solution
12-16-2022 06:23 AM
MartinBrandi
New Contributor II

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 🙂

0 Kudos
6 Solutions

Accepted Solutions
DerekKonofalski
Occasional Contributor

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?

View solution in original post

DerekKonofalski
Occasional Contributor

@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.

View solution in original post

DerekKonofalski
Occasional Contributor

@MartinBrandi - Are you doing that intentionally? My response is not a solution to your question.  😕

View solution in original post

DerekKonofalski
Occasional Contributor
DerekKonofalski
Occasional Contributor

Seems like @MartinBrandi is just a bot that auto-accepts all responses as solutions and gives them kudos.

View solution in original post

DerekKonofalski
Occasional Contributor

Thanks for the expert badge, @MartinBrandi. 🙄

View solution in original post

11 Replies
ZacharySutherby
Esri Regular Contributor

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. 

Thank you,
Zach
MartinBrandi
New Contributor II

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);
}

0 Kudos
rsun_TQB
Occasional Contributor III

Hi @MartinBrandi 

Do you know how to do basic authentication using Microsoft Power Automate - HTTP Action?
 
Thanks,
Reno
0 Kudos
DerekKonofalski
Occasional Contributor

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?

DerekKonofalski
Occasional Contributor

@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.

DerekKonofalski
Occasional Contributor

@MartinBrandi - Are you doing that intentionally? My response is not a solution to your question.  😕

DerekKonofalski
Occasional Contributor

Are you a bot, @MartinBrandi?

DerekKonofalski
Occasional Contributor

Seems like @MartinBrandi is just a bot that auto-accepts all responses as solutions and gives them kudos.

DerekKonofalski
Occasional Contributor

Thanks for the expert badge, @MartinBrandi. 🙄