Skip to content

Instantly share code, notes, and snippets.

@tkssharma
Created October 1, 2017 03:55
Show Gist options
  • Save tkssharma/cfe1c686198e919ed6cdd461b636ebe8 to your computer and use it in GitHub Desktop.
Save tkssharma/cfe1c686198e919ed6cdd461b636ebe8 to your computer and use it in GitHub Desktop.
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