From 225e67a1e5314958c59f7189d1a7fe0335606dc9 Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Thu, 13 Jun 2024 14:28:03 +0100 Subject: [PATCH] fix: allow POST requests without body / content-type header (#6881) This is not possible for most routes as request will fail schema validation --- packages/api/src/utils/server/handler.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/api/src/utils/server/handler.ts b/packages/api/src/utils/server/handler.ts index 162f68f25c05..fc2d1fe6ae6e 100644 --- a/packages/api/src/utils/server/handler.ts +++ b/packages/api/src/utils/server/handler.ts @@ -65,15 +65,22 @@ export function createFastifyHandler( returnBytes: responseWireFormat === WireFormat.ssz, }); } else { + let requestWireFormat: WireFormat; const contentType = req.headers[HttpHeader.ContentType]; - if (contentType === undefined) { - throw new ApiError(400, "Content-Type header is required"); + if (contentType === undefined && req.body === undefined) { + // Default to json parser if body is omitted. This is not possible for most + // routes as request will fail schema validation before this handler is called + requestWireFormat = WireFormat.json; + } else { + if (contentType === undefined) { + throw new ApiError(400, "Content-Type header is required"); + } + const requestMediaType = parseContentTypeHeader(contentType); + if (requestMediaType === null) { + throw new ApiError(415, `Unsupported media type: ${contentType.split(";", 1)[0]}`); + } + requestWireFormat = getWireFormat(requestMediaType); } - const requestMediaType = parseContentTypeHeader(contentType); - if (requestMediaType === null) { - throw new ApiError(415, `Unsupported media type: ${contentType.split(";", 1)[0]}`); - } - const requestWireFormat = getWireFormat(requestMediaType); const {onlySupport} = definition.req as RequestWithBodyCodec; if (onlySupport !== undefined && onlySupport !== requestWireFormat) {