Skip to content

Instantly share code, notes, and snippets.

@principalweb
Created January 30, 2019 18:13
Show Gist options
  • Save principalweb/3c7a9557d3ef2a268d1d6e3b8e8af1bb to your computer and use it in GitHub Desktop.
Save principalweb/3c7a9557d3ef2a268d1d6e3b8e8af1bb to your computer and use it in GitHub Desktop.
Service to consume API
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import * as lodash from 'lodash';
@Injectable()
export class HttpService {
public authFail: Boolean = false;
constructor(
private http: HttpClient
) { }
public get(url, options?): Observable<any> {
return this.http.get(url, options);
}
public post(url, data = null, options?): Observable<any> {
return this.http.post(url, data, options);
}
public postFile(url, data, options = {}): Observable<any> {
const headers = new HttpHeaders();
headers.append('Content-Type', 'multipart/form-data');
headers.append('Accept', 'application/json');
return this.http.post(url, data, lodash.merge({}, options, { headers }));
}
public put(url, data = null, options?): Observable<any> {
return this.http.put(url, data, options);
}
public del(url, options?): Observable<any> {
return this.http.delete(url, options);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment