Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
http: how to get json from batch response?

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&#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........
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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().

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda