简体   繁体   English

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

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

How do you get json from batch request?你如何从批处理请求中获取json?

I'm getting the following error if I console.log my fetch response.json() :如果我 console.log my fetch response.json()我会收到以下错误:

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

If I dive into network sources to see the response (by the way it has status 200) in devTools I see the following line producing the error:如果我深入网络资源以查看 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

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

My fetch() call has following headers:我的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())

each batch request inside body has Content-Type: application/json正文中的每个批处理请求都有Content-Type: application/json

EDIT:编辑:
I want the following response.我想要以下回复。

// 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........
}

If someone else faces same issue:如果其他人面临同样的问题:

I thought there was singular response with a body containing JSON.我认为有一个包含 JSON 的主体的单一响应。 Technically there is a singular response but it's formatted as multiple responses.从技术上讲,有一个单一的响应,但它被格式化为多个响应。

Multipart responses actually contain multiple http responses each with an header and seperator --[foobar] .多部分响应实际上包含多个 http 响应,每个响应都有一个header和分隔符--[foobar] They aren't all concatenated inside a containing body and it needs to be manually tranformed into useable data by applying string/array methods or using a library.它们并非都连接在包含主体内,需要通过应用字符串/数组方法或使用库将其手动转换为可用数据。

There is no in-build solution for this equivalent to response=>response.json() .没有与response=>response.json()等效的内置解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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