簡體   English   中英

http:如何從批處理響應中獲取 json?

[英]http: how to get json from batch response?

你如何從批處理請求中獲取json?

如果我 console.log my fetch response.json()我會收到以下錯誤:

Uncaught (in promise) SyntaxError: Unexpected number in JSON at position 3

如果我深入網絡資源以查看 devTools 中的響應(by the way it has status 200) ,我會看到以下行產生錯誤:

--batch_SuWHKrAohxj1o_r1qo6yzaAu-gfaqQ1p <-- producing error. Red lines underneath.
Content-Type: application/http
Content-ID: response-

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Vary: Origin
Vary: X-Origin
Vary: Referer

{
..........
..........
.........
}

我的fetch()調用具有以下標題:

  headers.append('Authorization', `Bearer ${token}`);
  headers.append('Host', 'www.googleapis.com');
  headers.append('Content-Type', 'multipart/mixed; boundary="deel_foo"');
  headers.append('Accept-Encoding', 'gzip');
  headers.append('Accept', 'application/json');
fetch(.....).then(response) => response.json())

正文中的每個批處理請求都有Content-Type: application/json

編輯:
我想要以下回復。

// stripped off the following:
--batch_K_aZ7aQTR91ApoeMUayvTEJhZBs6PW9n
Content-Type: application/http
Content-ID: response-

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Vary: Origin
Vary: X-Origin
Vary: Referer
// stripped off till here.
{   // I want this part, as it's valid JSON.
  "id": "17vegjeogjeogje44",
  "threadId": "17vjorgjerogjerogjer",
  "labelIds": [
    "CATEGORY_UPDATES",
    "INBOX"
  ],
  "snippet": "Don&#39;t miss this idea and many more. Stash what matters to you and inspire others. \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c \u200c",
  "payload": {
    "partId": "",
    "mimeType": "text/html",
    "filename": "",
    "headers": [
........
etc
........
etc........
}

如果其他人面臨同樣的問題:

我認為有一個包含 JSON 的主體的單一響應。 從技術上講,有一個單一的響應,但它被格式化為多個響應。

多部分響應實際上包含多個 http 響應,每個響應都有一個header和分隔符--[foobar] 它們並非都連接在包含主體內,需要通過應用字符串/數組方法或使用庫將其手動轉換為可用數據。

沒有與response=>response.json()等效的內置解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM