User382
User382

Reputation: 874

Basic authentification issue through XMLHttpRequest

I am trying to call an OutPan service to get barcode details.

var url = "https://api.outpan.com/v1/products/0796435419035";
var xmlhttp = new XMLHttpRequest();
var encodedString = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:";
xmlhttp.open("GET", url, true);

xmlhttp.setRequestHeader("Authorization", "Basic " + encodedString);
xmlhttp.send();
var temp = xmlhttp.responseXML;

encodedString is the API key Base64 conversion. Converting string to Base64 is not straightforward in Javascript. I have converted it using online tool and assigned that value to encodedString. However I get:

Failed to load resource: the server responded with a status of 401 (Unauthorized)

Reader.html:1 XMLHttpRequest cannot load https://api.outpan.com/v1/products/0796435419035. Invalid HTTP status code 401

Upvotes: 0

Views: 95

Answers (1)

dfogni
dfogni

Reputation: 798

You are forgetting the colon and the (empty) password.

The format for basic authetication is "Basic " + base64(username + ":" + password), so you need to encode your api key followed by a colon.

Upvotes: 1

Related Questions