fix(http): parse every +json media type as JSON (GitHub #36)
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>
This commit is contained in:
3 files changed
+9
-4
No files matched your search
+3
-1
@@ -74,7 +74,9 @@ export async function parseRequestBody(
|
||||
): Promise<ParseResult> {
|
||||
const type = mediaType(contentType);
|
||||
|
||||
if (buf !== undefined && type === 'application/json') {
|
||||
// `+json` is the structured-syntax suffix for JSON (RFC 6839): `application/problem+json`,
|
||||
// `application/vnd.api+json` and `application/json-patch+json` are JSON documents too.
|
||||
if (buf !== undefined && (type === 'application/json' || type.endsWith('+json'))) {
|
||||
try {
|
||||
return { body: JSON.parse(buf.toString('utf8')) };
|
||||
} catch {
|
||||
|
||||
Reference in new issue
Block a user