繁体   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