繁体   English   中英

为什么即使在 Postman 中返回,我也无法从 POST 请求中接收正文?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM