0

I've setup my api into web server, currently im unable to retrieve data using my app key but able to do so using postman, please do help

const httpOptions = {
      headers: new HttpHeaders({
        'APP_KEY': 'ABCDEFGHJ'

      })
    };

    this.http.get('<my_api_link>', httpOptions).subscribe((res) => {
      this.Result = JSON.stringify(res);
      console.log('Result', this.DataResult);
    }, (err) => {
      console.error(err.status);
      console.error(err.error); // Error message as string
      console.error(err.headers);
    });

Error message was unable to find app key

4 Answers 4

1

Try to set headers this way

const httpHeaders = new HttpHeaders()
  .set('APP_KEY', 'ABCDEFGHJ');

this.http.get('<my_api_link>', httpHeaders)
Sign up to request clarification or add additional context in comments.

6 Comments

shouldn't headers be part of options object?
@KiraAG this way is also valid
actually all these 3 method provided is the same, just abit different on the structure, but still unable to detect the app key
How are you checking that key is not getting detected?
i output the entered key at console DataResult {"0":401,"message":"App key not found, entered API_KEY = "}
|
1

Try this :

var token = 'ABCDEFGHJ';
const myHeaders = new HttpHeaders({ 'Content-Type': 'application/json', 'Authorization': token });
this.http.get('<my_api_link>', { headers: myHeaders });

Or

let headers = new HttpHeaders();
headers = headers.set('APP_KEY','ABCDEFGHJ');

Comments

1

Modifying your code snippet :

const httpOptions = {
  headers: new HttpHeaders().set('APP_KEY', 'ABCDEFGHJ')
};

this.http.get('<my_api_link>', httpOptions).subscribe((res) => {
  this.Result = JSON.stringify(res);
  console.log('Result', this.DataResult);
}, (err) => {
  console.error(err.status);
  console.error(err.error); // Error message as string
  console.error(err.headers);
});

Comments

0

import {HttpClient} from '@angular/common/http';

constructor(private _httpClient: HttpClient) {

// some code

}

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.