1

I am trying to pass headers in my get request using Angular 7. The headers is an Authorization token. I just tried to do the following

  this.http.get('url', {Authorization: 'Basic xzeydyt=='});

I am now getting the following error

Argument of type '{ Authorization: string; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]

2
  • Pass your header section like that { headers: headers } Commented Mar 7, 2019 at 6:27
  • Did you try reading Angular docs for the same? angular.io/guide/http#adding-headers Commented Mar 7, 2019 at 6:45

6 Answers 6

3

Example 1:

 // Example Get request


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

     const httpOptions = {

     headers: new HttpHeaders({

      'Content-Type':  'application/json',

      'Authorization':  'token'

     })};

     get_data (): Observable<any> {

         const Url = `${serviceurl}`;

         return this.http.get<any>(Url, httpOptions)

         .pipe(map(res => res))

         .catch(err => err);
      }

Example 2:

   // Example Post request

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

       const httpOptions = {

       headers: new HttpHeaders({

       'Content-Type':  'application/json',

       'Authorization':  'token'

      })};

      Post_data (customer_name): Observable<any> {

       const Url = `${serviceurl}`;

       const body = JSON.stringify(

       {

        customer_name: customer_name 

         });

        return this.http.post<any>(Url, body, httpOptions)

        .pipe(map(res => res))

        .catch(err => err);
  }
Sign up to request clarification or add additional context in comments.

Comments

2

You will need to create a new instance of HttpHeaders and pass that onto the parameters of the get method.

You will need to import HttpHeaders as well to be able to use it

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

So you will build up the headers as follows:

let headers = new HttpHeaders();
headers = headers.set('Authorization', 'Basic xzeydyt==');

You can then pass that onto the get method as follows:

this.http.get('url', { headers: headers });

Comments

2

You can achieve this using this approach,

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

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'application/json',
    'Authorization': 'my-auth-token'
  })
};

example GET request,

addBook (book: Book): Observable<Book> {
  return this.http.get<Hero>(this.bookUrl, httpOptions)
    .pipe(
      catchError(this.handleError('addBook', book))
    );
}

Comments

1

You add headers in api requests by defining headers as the object of HttpHeaders type The example in your case :


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


      `const httpHeaders = {headers: new HttpHeaders({                   
                                    Authorization: 'Basic xzeydyt=='
                                                })
                      };

      this.http.get('url',httpHeaders);`

Comments

0

I have an example code for the following question.

 public getAll() :Observable<employee[]> {
        const url = "http://localhost:8080/getAll";
        const headers = new HttpHeaders({
          Authorization: "Basic " + btoa("user:secret123")
        });
        return this.http.get<employee[]>(url, { headers });
      }

Hope it works!

Comments

0

You need to Impoer Headers Like this.

import { Http, RequestOptions, Headers } from '@angular/http';

This is your function code for get request

let headers = new Headers({Authorization: 'Basic xzeydyt=='});
let options = new RequestOptions({ headers: headers });

this.http.get(url, options)
 .map(data => {

 });

1 Comment

I think these imports are deprecated

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.