Console says unauthorized when sending request


#1

I am sending a GET request to retrieve all mesh networks around me but when I do I get an error saying “401… Unauthorized” and I believe I am getting this error because I didn’t include my token and app-id. Here is my code
this.http.get(‘https://cloud.estimote.com/v2/mesh’, {}, {})
.then(data => {

console.log(data.status);
console.log(data.data); // data received by server
console.log(data.headers);

})
.catch(error => {

console.log(error.status);
console.log(error.error); // error message as string
console.log(error.headers);

});


#2

That pretty much answers your question :smiley:

You’ll need to consult the documentation of the library you’re using to make the request, to learn how to add the Basic HTTP Authorization header. (If you let me know what library it is, I can help.)


#3

I am currently using


How would I add the token and app-id to the code?


#4

Then I think it’s gonna be something like this:

const appId = "xxx";
const appToken = "xxx";

// HTTP Basic Auth string = "username:password"
// here username is appId, and password is appToken
const authString = appId + ":" + appToken;

// then we need to encode this as Base64
const base64AuthString = btoa(authString);

const httpOptions = {
  headers: new HttpHeaders({
    'Authorization': 'Basic ' + base64AuthString
  })
};

http.get("...", httpOptions);

#5

Hmm I’ve been trying it and instead of getting an error, it displays nothing. Do you think the link I have for my http.get is correct?


#6

Any ideas @heypiotr ?