Authorization header not working in nativescript


#1

I am making a request to a RestAPI made on DjangoRestFramework using NativeScript for Angular with the next code:

getImages() {
    let headers = new Headers();
    headers.append("Content-Type", "application/json");
    headers.append('Authorization', 'Basic YWRtaW5jaHVwaXRvczpDaHVwaXRvczIwMTguKg==');

    return this.http.get(
      Config.apiUrl + "images",
      { headers: headers}
    )
    .map(response => response.json())
    .catch(this.handleErrors);
  }

However, the API sends the next message:

{“detail”:“Authentication credentials were not provided.”}

I have tested the request using PostMan an it works so I don’t know what I’m missing. Thanks in advance for any help!


#2

hi, can you try something like the below, using http.request and passing in the headers like thus. The other issue may have to do with encoding your key properly: https://stackoverflow.com/questions/16282179/how-to-convert-a-public-key-to-a-base64-public-key. I have to do that when using MailGun. Try one or both of these?

http.request({
         url: "https://api.clarifai.com/v2/models/aaa03c23b3724a16a56b629203edc62c/outputs",
         method: "POST",
            headers: {
                 "Content-Type": "application/json",
                  "Authorization": "Key e65d726322664ad1b046fb5ee074114c",
            },
                            ...

#3

I was having an issue (Java errors on Android) when using http.getImage with basic auth. The code I was using all declared new Headers() and did headers.set(). Switching to the suggested method of inline, manually constructed headers object in the options object worked for me.

This was a solution for me, thank you.