Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created June 11, 2020 02:47
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 parzibyte/0ef07e5555a0e87c9e01e93fbee06b21 to your computer and use it in GitHub Desktop.
Save parzibyte/0ef07e5555a0e87c9e01e93fbee06b21 to your computer and use it in GitHub Desktop.
import {Injectable} from '@angular/core';
import {environment} from "../environments/environment";
@Injectable({
providedIn: 'root'
})
export class HttpService {
rutaServidor = environment.baseUrl;
constructor() {
}
public async post(ruta: string, payload: any) {
const respuestaRaw = await fetch(this.rutaServidor + ruta, {
body: JSON.stringify(payload),
headers: {
"Content-Type": "application/json",
},
method: "POST",
credentials: "include",
});
return await respuestaRaw.json();
}
public async formdata(ruta: string, payload: FormData) {
const respuestaRaw = await fetch(this.rutaServidor + ruta, {
body: payload,
method: "POST",
});
return await respuestaRaw.json();
}
async get(ruta: string) {
// Por defecto se hace una petición GET
const respuestaRaw = await fetch(this.rutaServidor + ruta, {
credentials: "include",
});
return await respuestaRaw.json();
}
async delete(ruta: string) {
const respuestaRaw = await fetch(this.rutaServidor + ruta, {
credentials: "include",
method: "DELETE",
});
return await respuestaRaw.json();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment