Last active
March 8, 2017 08:57
-
-
Save 0x1ad2/68f6270e8acccc0ebde02f0fd82b70de to your computer and use it in GitHub Desktop.
HTTP GET example Angular
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 {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