¿Cómo se obtiene json de la solicitud por lotes?
Recibo el siguiente error si consuelo. registro mi fetch response.json() :
Uncaught (in promise) SyntaxError: Unexpected number in JSON at position 3
Si me sumerjo en las fuentes de la red para ver la respuesta (by the way it has status 200) en devTools, veo la siguiente línea que produce el 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 { .......... .......... ......... } Mi llamada fetch() tiene los siguientes encabezados:
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()) cada solicitud por lotes dentro del cuerpo tiene Content-Type: application/json
EDITAR:
Quiero la siguiente respuesta.
// 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........ }Si alguien más enfrenta el mismo problema:
Pensé que había una respuesta singular con un cuerpo que contenía JSON. Técnicamente, hay una respuesta singular, pero está formateada como respuestas múltiples.
Las respuestas de varias partes en realidad contienen varias respuestas http, cada una con un header y un separador --[foobar] . No todos están concatenados dentro de un cuerpo contenedor y deben transformarse manualmente en datos utilizables mediante la aplicación de métodos de cadena/matriz o el uso de una biblioteca.
No existe una solución integrada para este equivalente a response=>response.json() .