Created
November 13, 2016 11:21
-
-
Save AndreasLoukakis/3db56109244cded157a748e80a1f6501 to your computer and use it in GitHub Desktop.
Wrap Angular 2 http service with custom headers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable} from '@angular/core'; | |
import { Http, Headers, Response } from '@angular/http'; | |
import { XCSRF } from './../services/tokens' | |
@Injectable() | |
export class HttpClient { | |
constructor(http: Http) { | |
this._http = http; | |
} | |
private _http: Http; | |
private createAuthorizationHeader(headers:Headers) { | |
let token = XCSRF.toString().replace('Token ', ''); | |
headers.append('Accept', 'application/json'); | |
headers.append('X-Csrf-Token', token); | |
} | |
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 | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment