简体   繁体   中英

Unable to detect http headers

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

Try to set headers this way

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

this.http.get('<my_api_link>', httpHeaders)

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');

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);
});

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

constructor(private _httpClient: HttpClient) {

// some code

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM