HEAD response with a JSON content-type throws "Unexpected end of JSON input"
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
- Ambito
- backend-api-design
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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di digitalbazaar/http-client
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
digitalbazaar/http-client#59 ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
digitalbazaar/http-client#45 · 1 commento ·
-
CVE: 2024-24750 in undiciAperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 25/100
digitalbazaar/http-client#44 ·
-
Keep an eye on `node:undici` (or similar) exposing `undici` in `node` and remove `undici` dependencyAperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 30/100
digitalbazaar/http-client#43 ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 15/100
digitalbazaar/http-client#37 ·
Tutte le issue di digitalbazaar/http-client
Issue simili
-
needs-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
coder/claudecode.nvim#588 ·
I maintainer di solito rispondono entro 1 giorno
-
Selectmenu changes selection after its native select is disabled while openForse già presa @Xapollon430 l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
-
Ruby: The link in the Blocks lesson points to an external article that isn't at that address anymoreAperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 84/100
TheOdinProject/curriculum#31529 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
topmonks/hlidac-shopu#3687 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
ONLYOFFICE/DocumentServer#3912 · 1 commento ·