http:如何从批处理响应中获取json



如何从批处理请求中获取json?

如果我控制台.log我的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(),没有内部构建的解决方案。

最新更新