How do you get json from batch request?
I'm getting the following error if I 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:
--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:
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
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'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. 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]
. 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()
.