RRB
RRB

Reputation: 2116

Pass headers in a get request using Angular 7

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]

Upvotes: 1

Views: 16031

Answers (6)

arul prince
arul prince

Reputation: 303

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

Upvotes: 3

rcanpahali
rcanpahali

Reputation: 2643

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

Upvotes: 2

Ashutosh Baghel
Ashutosh Baghel

Reputation: 11

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

Upvotes: 1

Ushma Joshi
Ushma Joshi

Reputation: 459

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

 });

Upvotes: 0

Tachyon
Tachyon

Reputation: 2411

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

Upvotes: 2

Anil Kumar Reddy A
Anil Kumar Reddy A

Reputation: 648

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!

Upvotes: 0

Related Questions