簡體   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