[英]Why is the POST request response data null in CasperJS even though same request shows data in Postman
[英]Why can't I receive the body from a POST request even though it is returned in Postman?
我在 JavaScript 中使用fetch()
API 从我的 flask 后端服务器检索信息。 我在 postman 中测试了相同的 URL 和端点,我收到了响应正文。 但是,当我通过fetch()
执行相同的 POST 并使用async/await
处理响应时,我得到body: undefined
在客户端。 下面是代码:
const result = await fetch(`${BACKEND_URL}/auth`, {
method: "POST",
body: newUserBasicString, // some payload
headers: {
"Content-type": "application/json",
},
});
console.log(JSON.stringify(result));
BACKEND_URL
是转发的 ngrok https url。 为什么我没有收到尸体?
您仍然需要处理 fetch api 返回的数据,默认情况下它不知道如何处理正文。 如果你想内联,这应该返回你想要的。
const result = await fetch(`${BACKEND_URL}/auth`, {
method: "POST",
body: newUserBasicString, // some payload
headers: {
"Content-type": "application/json",
},
}).then(response => response.json())
// .json() for application/json response
// .text() for application/text response
console.log(JSON.stringify(result));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.