Set basic authentication in header in javascript

Not applicable

Hi All,

I have an requirement to set Basic authentication in the HTTP header. This is required within a javascript because I need to call edge management api through JavaScript.

Can anyone please provide me a working example?

I did the following way

var headers = {Authorization : 'Bearer c3V2b2ppd...xxxxx....=='};

var url = "https://api.enterprise.apigee.com/v1/organizations/rssoftwarestaging/apps/"

and call the URL like below format

var myRequest = new Request(url,"GET",headers);

var exchange = httpClient.send(myRequest);

But call is failing

I got the below error from trace log

{"headers":{"Connection":"keep-alive","Content-Length":"0","Date":"Thu, 12 Jan 2017 10:06:22 GMT","Server":"Apigee LB","WWW-Authenticate":"Basic realm=\"organizations/rssoftwarestaging/apps/a937ece1-a0be-4475-bfea-e11fdeb279c6\""},"content":"","status":{"message":"Unauthorized","code":"401"}}

Please suggest me the correct way to set the header.

Regards,

Suvojit

0 5 48.5K
5 REPLIES 5

Not applicable

@Suvojit Chandra

The syntax for basic authentication is { Authorization: Basic c3V2b2pxxxxxxx==}

Instead of Bearer try with Basic.

You should change your password immediately.

You have posted the base64 encoding of your credentials, and the world can see them.

I've since modified your post, but there's no telling how many people have seen your credentials. Also if you have re-used that password anywhere else, you should change it there, too.

Not applicable

@Suvojit Chandra While posting any code or any question on community make sure that you are not putting any sensitive data or any client specific information on community.

and also one more thing is that

var myRequest = new Request(url,"GET",headers,"");

Not applicable

Hi Suvojit,

var headers = {Authorization : 'Bearer c3V2b2ppd...xxxxx....=='};

Bearer is for Oauth token authentication

change to

var headers = {Authorization : 'Basic c3V2b2ppd...xxxxx....=='};