Created
October 1, 2017 03:55
-
-
Save tkssharma/cfe1c686198e919ed6cdd461b636ebe8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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, Request, BaseRequestOptions, RequestMethod, ResponseContentType} from '@angular/http'; | |
import {Observable} from 'rxjs/Rx'; | |
@Injectable() | |
export class HttpClient { | |
constructor(private http: Http) {} | |
post(url: string, body: any = {}) { | |
return this.request(url, RequestMethod.Post, body); | |
} | |
get(url: string) { | |
return this.request(url, RequestMethod.Get); | |
} | |
put(url: string, body: any) { | |
return this.request(url, RequestMethod.Put, body); | |
} | |
postAndGetBlob(url: string, body: any = {}) { | |
return this.request(url, RequestMethod.Post, body, true); | |
} | |
private request(url: string, method: RequestMethod, body?: any, isBlob = false): Observable<Response> { | |
const options = new BaseRequestOptions(); | |
options.url = url; | |
options.method = method; | |
options.body = body; | |
if(isBlob) { | |
options.responseType = ResponseContentType.Blob; | |
} | |
const request = new Request(options); | |
return this.http.request(request) | |
.catch((error: any) => this.onErrorHandler(error)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment