Skip to content

Instantly share code, notes, and snippets.

@rexar1988
Last active October 8, 2018 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rexar1988/68f09c497daa4ccaf474284d52bedf6f to your computer and use it in GitHub Desktop.
Save rexar1988/68f09c497daa4ccaf474284d52bedf6f to your computer and use it in GitHub Desktop.
Angular 2+: Http Service
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { environment } from '../../../environments/environment';
@Injectable()
export class HttpCallService {
link = environment.serverDomain;
constructor(private httpClient: HttpClient) {}
public call(method: string, postfixLink: string, params?: any) {
let resultCall = false;
switch (method) {
case 'GET':
resultCall = this.httpClient.get(this.link + postfixLink);
break;
case 'POST':
resultCall = this.httpClient.post(this.link + postfixLink, params);
break;
case 'DELETE':
resultCall = this.httpClient.delete(this.link + postfixLink);
break;
case 'PUT':
resultCall = this.httpClient.put(this.link + postfixLink, JSON.stringify(params));
break;
default:
console.log('unknown method: ' + method);
}
return resultCall;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment