From bdd02b128c0c6fef24b423c4bf5a2186f8ffd3fa Mon Sep 17 00:00:00 2001 From: jakecastelli <38635403+jakecastelli@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:51:55 +1030 Subject: [PATCH] http2: add missing setHeaders Make it possible to invoke response.setHeaders() on the Http2ServerResponse object, matching the http.OutgoingMessage API. Fixes: https://github.com/nodejs/node/issues/51573 Refs: https://github.com/nodejs/node/pull/51576 Signed-off-by: jakecastelli <38635403+jakecastelli@users.noreply.github.com> Signed-off-by: Mikhail Leliakin Co-authored-by: Mikhail Leliakin Assisted-by: Cursor PR-URL: https://github.com/nodejs/node/pull/67597 Reviewed-By: Jason Zhang Reviewed-By: Matteo Collina --- doc/api/http2.md | 41 ++++ lib/internal/http2/compat.js | 40 ++++ .../parallel/test-http2-server-set-headers.js | 204 ++++++++++++++++++ 3 files changed, 285 insertions(+) create mode 100644 test/parallel/test-http2-server-set-headers.js diff --git a/doc/api/http2.md b/doc/api/http2.md index 2729820e5d8..03f971a0754 100644 --- a/doc/api/http2.md +++ b/doc/api/http2.md @@ -4911,6 +4911,45 @@ const server = http2.createServer((req, res) => { }); ``` +#### `response.setHeaders(headers)` + + + +* `headers` {Headers|Map} + +Sets multiple header values for implicit headers. +`headers` must be an instance of [`Headers`][] or `Map`. +If a header already exists in the to-be-sent headers, +its value will be replaced. + +```js +const headers = new Headers({ foo: 'bar' }); +response.setHeaders(headers); +``` + +or + +```js +const headers = new Map([['foo', 'bar']]); +response.setHeaders(headers); +``` + +When headers have been set with [`response.setHeaders()`][], +they will be merged with any headers passed to [`response.writeHead()`][], +with the headers passed to [`response.writeHead()`][] given precedence. + +```js +// Returns content-type = text/plain +const server = http2.createServer((req, res) => { + const headers = new Headers({ 'Content-Type': 'text/html' }); + res.setHeaders(headers); + res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.end('ok'); +}); +``` + #### `response.setTimeout(msecs[, callback])`