Skip to content

Commit 4966560

Browse files
authored
fix(openapi-fetch): prevent body parsing for HEAD requests regardless of Content-Length (#2205)
* fix(openapi-fetch): prevent body parsing for HEAD requests regardless of Content-Length * chore: add changeset for HEAD method fix
1 parent 055f8f1 commit 4966560

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

.changeset/breezy-bottles-mix.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-fetch": patch
3+
---
4+
5+
Fix HEAD method requests to prevent body parsing regardless of Content-Length header value

packages/openapi-fetch/src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export default function createClient(clientOptions) {
209209
}
210210

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

packages/openapi-fetch/test/http-methods/head.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,16 @@ describe("HEAD", () => {
1313
await client.HEAD("/resources/{id}", { params: { path: { id: 123 } } });
1414
expect(method).toBe("HEAD");
1515
});
16+
17+
test("handles HEAD requests with non-zero Content-Length without parsing the body", async () => {
18+
const client = createObservedClient<paths>({}, async () => {
19+
return new Response(null, {
20+
headers: { "Content-Length": "42", "Content-Type": "application/json" },
21+
status: 200,
22+
});
23+
});
24+
const result = await client.HEAD("/resources/{id}", { params: { path: { id: 123 } } });
25+
expect(result.data).toBeUndefined();
26+
expect(result.response.ok).toBe(true);
27+
});
1628
});

0 commit comments

Comments
 (0)