HEAD response with a JSON content-type throws "Unexpected end of JSON input"

Aperta Adatta ai principianti
#60 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript, node.js

Direzione di ricerca

La issue rimanda a _handleResponse in lib/httpClient.js (righe 105–114). Leggi come decide di analizzare i corpi delle risposte, quindi controlla i test esistenti per httpClient.head(url) e httpClient(url, {method: 'HEAD'}). Il lavoro è terminato quando entrambe le forme vengono risolte correttamente con response.data undefined per un tipo di contenuto JSON.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

A HEAD request to an endpoint that serves JSON rejects with SyntaxError: Unexpected end of JSON input, even though the request succeeded.

A HEAD response carries the same headers as the GET, including Content-Type: application/json, but has no body. _handleResponse decides whether to parse from the content-type alone, so it calls response.json() on the empty body:

https://github.com/digitalbazaar/http-client/blob/main/lib/httpClient.js#L105-L114

Reproduction

Seen with 4.4.0 on Node 24.

import {createServer} from 'node:http';
import {httpClient} from '@digitalbazaar/http-client';

const server = createServer((req, res) => {
  const body = JSON.stringify({ok: true});
  res.writeHead(200, {
    'content-type': 'application/json',
    'content-length': Buffer.byteLength(body)
  });
  // a HEAD response carries the GET's headers and no body
  res.end(req.method === 'HEAD' ? undefined : body);
});
await new Promise(resolve => server.listen(0, resolve));
const url = `http://localhost:${server.address().port}/`;

console.log('GET ', (await httpClient.get(url)).data);
try {
  console.log('HEAD', (await httpClient.head(url)).status);
} catch(e) {
  console.log('HEAD threw:', e.name, '-', e.message);
}
server.close();

Output:

GET  { ok: true }
HEAD threw: SyntaxError - Unexpected end of JSON input
Expected

The HEAD resolves with the response, and response.data is undefined.

Workaround

Passing parseBody: false avoids it. That only helps a caller that controls the options, which is not the case when the request is made through a wrapper such as ezcap's ZcapClient.request().

Suggested fix

Skip the body parse when the request method is HEAD, both for httpClient.head(url) and for the direct httpClient(url, {method: 'HEAD'}) form. Happy to send a PR.

Lingua principale
JavaScript
Stelle
2
Fork
8
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di digitalbazaar/http-client

Tutte le issue di digitalbazaar/http-client

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.