Skip to content

Commit

Permalink
fix: allow POST requests without body / content-type header (#6881)
Browse files Browse the repository at this point in the history
This is not possible for most routes as request will fail schema validation
  • Loading branch information
nflaig committed Jun 13, 2024
1 parent f4460cd commit 225e67a
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions packages/api/src/utils/server/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,22 @@ export function createFastifyHandler<E extends Endpoint>(
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<E>;
if (onlySupport !== undefined && onlySupport !== requestWireFormat) {
Expand Down

0 comments on commit 225e67a

Please sign in to comment.