Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - Set headers for every request

Tags:

angular

I need to set some Authorization headers after the user has logged in, for every subsequent request.


To set headers for a particular request,

import {Headers} from 'angular2/http'; var headers = new Headers(); headers.append(headerName, value);  // HTTP POST using these headers this.http.post(url, data, {   headers: headers }) // do something with the response 

Reference

But it would be not be feasible to manually set request headers for every request in this way.

How do I set the headers set once the user has logged in, and also remove those headers on logout?

like image 578
Avijit Gupta Avatar asked Dec 25 '15 15:12

Avijit Gupta


People also ask

How do you pass headers in angular HTTP?

There are two ways by which we can add the headers. One, we add the HTTP Headers while making a request. The second way is to use the HTTP interceptor to intercept all the Requests and add the Headers. In both cases, we use the httpHeaders configuration option provided by angular HttpClient to add the headers.

What would you use to attach the same header to all your HTTP calls?

Angular Interceptors technique comes in handy when you need to transform each request data, for instance, send an authorization token header with every HTTP request.

How do I add a header to my HTTP request?

To add custom headers to an HTTP request object, use the AddHeader() method. You can use this method multiple times to add multiple headers.

What are headers in HTTP requests?

An HTTP header is a field of an HTTP request or response that passes additional context and metadata about the request or response. For example, a request message can use headers to indicate it's preferred media formats, while a response can use header to indicate the media format of the returned body.


1 Answers

To answer, you question you could provide a service that wraps the original Http object from Angular. Something like described below.

import {Injectable} from '@angular/core'; import {Http, Headers} from '@angular/http';  @Injectable() export class HttpClient {    constructor(private http: Http) {}    createAuthorizationHeader(headers: Headers) {     headers.append('Authorization', 'Basic ' +       btoa('username:password'));    }    get(url) {     let headers = new Headers();     this.createAuthorizationHeader(headers);     return this.http.get(url, {       headers: headers     });   }    post(url, data) {     let headers = new Headers();     this.createAuthorizationHeader(headers);     return this.http.post(url, data, {       headers: headers     });   } } 

And instead of injecting the Http object you could inject this one (HttpClient).

import { HttpClient } from './http-client';  export class MyComponent {   // Notice we inject "our" HttpClient here, naming it Http so it's easier   constructor(http: HttpClient) {     this.http = httpClient;   }    handleSomething() {     this.http.post(url, data).subscribe(result => {         // console.log( result );     });   } } 

I also think that something could be done using multi providers for the Http class by providing your own class extending the Http one... See this link: http://blog.thoughtram.io/angular2/2015/11/23/multi-providers-in-angular-2.html.

like image 99
Thierry Templier Avatar answered Sep 22 '22 05:09

Thierry Templier