Skip to content

Instantly share code, notes, and snippets.

@0x1ad2
Last active March 8, 2017 08:57
Show Gist options
  • Save 0x1ad2/68f6270e8acccc0ebde02f0fd82b70de to your computer and use it in GitHub Desktop.
Save 0x1ad2/68f6270e8acccc0ebde02f0fd82b70de to your computer and use it in GitHub Desktop.
HTTP GET example Angular
import {Headers, Http, Response} from "@angular/http";
export class GenericClass {
constructor(public http: Http) {}
public MyHttpPostMethod() {
// create a new promise and resolve it if possible
return new Promise((resolve) => {
// set url
let url: string = 'http://apiurl.io/v1/products';
// set headers
let headers: Headers = new Headers();
headers.append('Content-Type', 'application/json');
// set payload
let payload: Object = {
a: 1,
b: '2'
};
// use Angular 2 HTTP module to execute HTTP POST method
this.http.post(url, payload, {
headers: this.headers
// map the response and resolve it
}).map((res: Response) => res.json()).subscribe((response) => {
resolve(response);
}, (error) => {
console.log(error);
});
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment