HEAD response with a JSON content-type throws "Unexpected end of JSON input"
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 82/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- javascript, node.js
- Domain
- backend-api-design
Research direction
The issue points to _handleResponse in lib/httpClient.js (lines 105–114). Read how it chooses to parse response bodies, then check the existing tests for httpClient.head(url) and httpClient(url, {method: 'HEAD'}). Done when both forms resolve successfully with response.data undefined for a JSON content type.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- JavaScript
- Stars
- 2
- Forks
- 8
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from digitalbazaar/http-client
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
digitalbazaar/http-client#59 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
digitalbazaar/http-client#45 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
digitalbazaar/http-client#44 ·
-
Keep an eye on `node:undici` (or similar) exposing `undici` in `node` and remove `undici` dependencyOpen
Difficulty 3/5 1-2 days Newbie friendliness 30/100
digitalbazaar/http-client#43 ·
-
Difficulty 5/5 Over a week Newbie friendliness 15/100
digitalbazaar/http-client#37 ·
All issues in digitalbazaar/http-client
Similar issues
-
needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
coder/claudecode.nvim#588 ·
Maintainers usually reply within 1 day
-
Selectmenu changes selection after its native select is disabled while openPossibly taken @Xapollon430 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Ruby: The link in the Blocks lesson points to an external article that isn't at that address anymoreOpen
Difficulty 1/5 Under an hour Newbie friendliness 84/100
TheOdinProject/curriculum#31529 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
topmonks/hlidac-shopu#3687 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
ONLYOFFICE/DocumentServer#3912 · 1 comment ·