guy sandler
guy sandler

Reputation: 11

Angular2 http post get the body of the request

I'm wondering how can I get the body of an HTTP POST request in Angulars2. when i get back to the error method the body is an accessible.

return this.http.post(requestUrl, **body**, options) 
.map((res:Response) => res.json(),
    (err)=> {
        return {"errorObj":err, "requestBody":**body**}
    } )

Thanks.

Upvotes: 1

Views: 880

Answers (1)

esso
esso

Reputation: 40

You can test this. working. will give you the body request and url.

this.http.post(url, body, this.options).subscribe(
  data => {
    console.log('ok url:' + url);
    console.log('body:' +  JSON.stringify(body));
  },
  (err: HttpErrorResponse) => {
    if (err.error instanceof Error) {
      // A client-side or network error occurred. Handle it accordingly.
      console.log('instanceof error url:' + url);
      console.log('body:' + JSON.stringify(body));
      console.log('An error occurred:', err.error.message);
    } else {
      // The backend returned an unsuccessful response code.
      // The response body may contain clues as to what went wrong,
      console.log('backend error url:' + url:' + url);
      console.log('body:' + JSON.stringify(body));
      let strBody = JSON.stringify(err);
      strBody = JSON.stringify(err.error);
      strBody = JSON.stringify(err.status);
      console.log(`Backend returned code ${err.status}, body was: ${err.error}`);
    }
  }
);

Upvotes: 0

Related Questions