2

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.

7
  • Where is your code where you calling your API with this headers? Commented Jun 18, 2018 at 10:36
  • This is what perfectly working for me: this.headers = new HttpHeaders({ 'Content-Type': 'application/json', 'appInstanceCode': '' + this.appCode, 'token': '' + this.token }); Commented Jun 18, 2018 at 10:38
  • Using HttpHeaders class Commented Jun 18, 2018 at 10:38
  • but new RequestOptions({ headers: headers }) is not taking httpHeader.. @PrashantPimpale Commented Jun 19, 2018 at 11:20
  • i am new to this. please help me Commented Jun 19, 2018 at 11:21

2 Answers 2

1
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

Sign up to request clarification or add additional context in comments.

Comments

0

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 => {
      })

1 Comment

glad it helps you!

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.