Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow POST requests without body / content-type header #6881

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading