Skip to content

fix(http2): allow GET and HEAD request bodies over h2#5058

Merged
mcollina merged 1 commit intonodejs:mainfrom
trivikr:http2-body-get
Apr 21, 2026
Merged

fix(http2): allow GET and HEAD request bodies over h2#5058
mcollina merged 1 commit intonodejs:mainfrom
trivikr:http2-body-get

Conversation

@trivikr
Copy link
Copy Markdown
Member

@trivikr trivikr commented Apr 18, 2026

This relates to...

Fixes: #5057

Rationale

HTTP/2 requests with a body on GET and HEAD currently fail with ERR_STREAM_WRITE_AFTER_END.

Changes

  • Change the HTTP/2 endStream decision to depend on whether the normalized request body is null, instead of special-casing GET and HEAD.
  • Add a regression test in test/http2-body.js covering HTTP/2 GET and HEAD requests with bodies.
  • Verify that both request bodies are delivered to the server and that the responses complete successfully.

Features

N/A

Bug Fixes

Fix ERR_STREAM_WRITE_AFTER_END for HTTP/2 GET and HEAD requests with a body

Breaking Changes and Deprecations

N/A

Status

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.10%. Comparing base (ec60a7c) to head (a80a688).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5058   +/-   ##
=======================================
  Coverage   93.10%   93.10%           
=======================================
  Files         110      110           
  Lines       35807    35807           
=======================================
  Hits        33339    33339           
  Misses       2468     2468           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina requested review from Copilot and metcoder95 and removed request for Copilot April 19, 2026 07:54
Assisted-by: OpenAI:gpt-5.4
Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com>
@mcollina
Copy link
Copy Markdown
Member

Cana you verify if those are allowed in http1?

@trivikr
Copy link
Copy Markdown
Member Author

trivikr commented Apr 21, 2026

Yes. Request bodies are allowed in GET and HEAD in http1 implementations

Undici example

import http from "node:http";
import { request } from "undici";

const server = http.createServer((req, res) => {
  let body = "";
  req.on("data", (c) => (body += c));
  req.on("end", () => {
    console.log(req.method, JSON.stringify(body));
    res.writeHead(200);
    res.end();
  });
});

server.listen(0, async () => {
  const { port } = server.address();
  const url = `http://127.0.0.1:${port}`;
  for (const method of ["GET", "HEAD"]) {
    await request(url, { method, body: `BODY:${method}` });
  }
  server.close();
});

Output

GET "BODY:GET"
HEAD "BODY:HEAD"

http1 example

import http from "node:http";
import { once } from "node:events";

const server = http.createServer((req, res) => {
  let body = "";
  req.on("data", (c) => (body += c));
  req.on("end", () => {
    console.log(req.method, JSON.stringify(body));
    res.end();
  });
});

server.listen(0);
await once(server, "listening");

const { port } = server.address();

await Promise.all(
  ["GET", "HEAD"].map(async (method) => {
    const body = `BODY:${method}`;
    const headers = { "content-length": Buffer.byteLength(body) };
    const req = http.request({ port, method, headers });

    req.end(body);

    const [res] = await once(req, "response");
    res.resume();
    await once(res, "end");
  }),
);

server.close();

Output

GET "BODY:GET"
HEAD "BODY:HEAD"

@mcollina mcollina merged commit 2e8bdd6 into nodejs:main Apr 21, 2026
35 checks passed
@trivikr trivikr deleted the http2-body-get branch April 21, 2026 15:19
@github-actions github-actions Bot mentioned this pull request May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTTP/2 GET/HEAD requests with a body fail with ERR_STREAM_WRITE_AFTER_END

3 participants