[英]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. 我想知道如何在Angulars2中获取HTTP POST请求的正文。 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. 谢谢。
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}`);
}
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.