Last active
October 8, 2018 16:48
-
-
Save rexar1988/68f09c497daa4ccaf474284d52bedf6f to your computer and use it in GitHub Desktop.
Angular 2+: Http Service
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 { 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