Reputation: 92
Getting 401 status even passing application id and key for each request
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded',});
let options = new RequestOptions({ headers: headers });
options.headers.set('Accept', "application/json,text/xml ");
options.headers.set('X-AYLIEN-NewsAPI-Application-ID', "ff6**d33");
options.headers.set('X-AYLIEN-NewsAPI-Application-Key', "b7445d942********7c06e");
API Call:
this.http.get(url)
.subscribe(res => {
resolve(res.json());
}, (err) => {
reject(err);
});
});
}
This is the code i am using to set value in header but it is not working.
Upvotes: 2
Views: 2353
Reputation: 92
getDataGetAuth(url) {
return new Promise((resolve, reject) => {
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
let options = new RequestOptions({ headers: headers });
options.headers.set('Content-Type', 'application/json' );
options.headers.set('Authorization', 'Bearer '+ sessionStorage.getItem("token") );
this.http.get( MainURL+url,options)
.subscribe(res => {
resolve(res.json());
}, (err) => {
reject(err);
});
});
}
This code is working fine for me
Upvotes: 1
Reputation: 10697
Try Below code:
In Service:
First :
import { HttpHeaders } from '@angular/common/http';
private url = "your_url";
private headers: any;
this.headers = new HttpHeaders({ 'Content-Type': 'application/json',
'your_key': '' + value, 'your_key': '' + value });
getRecords(): Promise<any>{
const url = `${this.url}/end_point`;
return this.http.get(url, {headers: this.headers })
.toPromise()
.catch(this.handleError);
}
In Component consume your service API call like:
this.your_service_obj.getRecords()
.then(response => {
// console.log(response)
},
err => {
})
Upvotes: 0