Skip to content

Instantly share code, notes, and snippets.

@tkssharma
Created October 7, 2017 17:29
Show Gist options
  • Save tkssharma/3512eddd41f298400a935b667db8fb63 to your computer and use it in GitHub Desktop.
Save tkssharma/3512eddd41f298400a935b667db8fb63 to your computer and use it in GitHub Desktop.
// Add a new comment
addComment (body: Object): Observable<Comment[]> {
let bodyString = JSON.stringify(body); // Stringify payload
let headers = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON
let options = new RequestOptions({ headers: headers }); // Create a request option
return this.http.post(this.commentsUrl, body, options) // ...using post request
.map((res:Response) => res.json()) // ...and calling .json() on the response to return data
.catch((error:any) => Observable.throw(error.json().error || 'Server error')); //...errors if any
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment