Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/breezy-bottles-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-fetch": patch
---

Fix HEAD method requests to prevent body parsing regardless of Content-Length header value
2 changes: 1 addition & 1 deletion packages/openapi-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export default function createClient(clientOptions) {
}

// handle empty content
if (response.status === 204 || response.headers.get("Content-Length") === "0") {
if (response.status === 204 || request.method === "HEAD" || response.headers.get("Content-Length") === "0") {
return response.ok ? { data: undefined, response } : { error: undefined, response };
}

Expand Down
12 changes: 12 additions & 0 deletions packages/openapi-fetch/test/http-methods/head.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,16 @@ describe("HEAD", () => {
await client.HEAD("/resources/{id}", { params: { path: { id: 123 } } });
expect(method).toBe("HEAD");
});

test("handles HEAD requests with non-zero Content-Length without parsing the body", async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great test!

const client = createObservedClient<paths>({}, async () => {
return new Response(null, {
headers: { "Content-Length": "42", "Content-Type": "application/json" },
status: 200,
});
});
const result = await client.HEAD("/resources/{id}", { params: { path: { id: 123 } } });
expect(result.data).toBeUndefined();
expect(result.response.ok).toBe(true);
});
});