fix(http): choose the body parser by the media type (GitHub #36) #162

Merged
chrnx merged 2 commits from fix/content-type-media-type into develop 2026-10-24 23:18:21 +00:00
Owner

parseRequestBody picked JSON, urlencoded or multipart with
contentType.includes() on the raw header. That is case-sensitive, so a
valid Application/JSON reached the handler as a string, and it
matches anywhere in the header, so a parameter could choose the
parser: text/plain; x=application/json was parsed as JSON.

The media type is now taken from the part before any ;, trimmed and
lowercased (RFC 9110 §8.3.1), and compared exactly. The boundary is
still read from the full header.

A type that merely contains one of the three no longer selects its
parser. application/json-patch+json used to be parsed as JSON and
now arrives as text, as other +json types always have. CHANGELOG
says so.

test/http.test.ts sends both cases from the issue, plus their
urlencoded equivalents, through the Node server.

parseRequestBody picked JSON, urlencoded or multipart with contentType.includes() on the raw header. That is case-sensitive, so a valid `Application/JSON` reached the handler as a string, and it matches anywhere in the header, so a parameter could choose the parser: `text/plain; x=application/json` was parsed as JSON. The media type is now taken from the part before any `;`, trimmed and lowercased (RFC 9110 §8.3.1), and compared exactly. The boundary is still read from the full header. A type that merely contains one of the three no longer selects its parser. `application/json-patch+json` used to be parsed as JSON and now arrives as text, as other `+json` types always have. CHANGELOG says so. test/http.test.ts sends both cases from the issue, plus their urlencoded equivalents, through the Node server.
chrnx added 2 commits 2026-10-24 23:15:55 +00:00
parseRequestBody picked JSON, urlencoded or multipart with
contentType.includes() on the raw header. That is case-sensitive, so a
valid `Application/JSON` reached the handler as a string, and it
matches anywhere in the header, so a parameter could choose the
parser: `text/plain; x=application/json` was parsed as JSON.

The media type is now taken from the part before any `;`, trimmed and
lowercased (RFC 9110 §8.3.1), and compared exactly. The boundary is
still read from the full header.

A type that merely contains one of the three no longer selects its
parser. `application/json-patch+json` used to be parsed as JSON and
now arrives as text, as other `+json` types always have. CHANGELOG
says so.

test/http.test.ts sends both cases from the issue, plus their
urlencoded equivalents, through the Node server.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
fix(http): parse every +json media type as JSON (GitHub #36)
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 20s
CI / build (pull_request) Successful in 29s
CI / runtimes (edge) (pull_request) Successful in 18s
CI / runtimes (deno) (pull_request) Successful in 33s
79925595b5
Exact matching left `application/json-patch+json` arriving as text,
where the old substring check had parsed it, and still left the other
`+json` types (`application/problem+json`, `application/vnd.api+json`)
as text, as they always were. `+json` is the structured-syntax suffix
for JSON (RFC 6839), so any media type ending in it is parsed as JSON.
`application/jsonx` and parameters that only mention JSON still are not.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
chrnx force-pushed fix/content-type-media-type from aef9640b50 to 79925595b5 2026-10-24 23:15:55 +00:00 Compare
chrnx merged commit d90e7ca2b0 into develop 2026-10-24 23:18:21 +00:00
Sign in to join this conversation.