Skip to content

doc: clarify http.request supports headers as array #58769

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Dinesh0007000
Copy link

This PR updates the documentation for http.request() to clarify that the headers option can also be provided as an array of key-value pairs, like ['Content-Type', 'text/plain', 'X-Custom', 'yes'].

This behavior aligns with response.writeHead(), but was previously undocumented in the http.request() API docs.

Fixes: #57986

This PR updates the documentation for `http.request()` to clarify that the `headers` option can also be an array of key-value pairs, similar to `response.writeHead`.

Fixes: nodejs#57986
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added doc Issues and PRs related to the documentations. http Issues or PRs related to the http subsystem. labels Jun 20, 2025
Copy link
Member

@pimterry pimterry left a comment

Choose a reason for hiding this comment

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

Thanks for contributing @Dinesh0007000! This is indeed officially supported and it's great to get this documented.

There's a few issues here to tidy up to get this ready to merge. I've added a comment on the markdown, and the commit message check is also failing due to line length. The guidelines for commit messages are here: https://github.com/nodejs/node/blob/HEAD/doc/contributing/pull-requests.md#commit-message-guidelines

doc/api/http.md Outdated
* `headers` {Object} An object containing request headers.
* `headers` {Object | Array} Request headers to send with the request. This can be:
* An object like `{ 'Content-Type': 'application/json' }`, or
* An array of key-value pairs like `[ 'Content-Type', 'text/plain', 'X-Custom', 'yes' ]`, similar to how headers are passed to `response.writeHead()`.
Copy link
Member

Choose a reason for hiding this comment

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

There's a series of extra spaces here (are passed), and this line is very long, which will be why the markdown linting is failing here. Can you fix that? You can run the markdown linting locally by running make lint-md.

"An array of key-value pairs" is not clear I think - that will make people think of [["key1", "value1"], ["key2", "value2"]] which is not what's happening. We should be specific that this is a flat array of values, not tuples, since this can be confusing.

I think it would be good to link to response.writeHead here, and request.rawHeaders as well (which uses the same format).

It might be better to keep the text inline here simple (a very short explanation of "headers object or flat array of raw headers" or similar) and then include the larger explanation & examples as a paragraph in the text below. What do you think? See what looks best - you can test the docs locally by running make docserve and then looking at http://localhost:8000.

Added a detailed explanation for how the 'headers' option in http.request
can also be passed as a flat array of alternating header names and values.

This provides clarity for beginners and aligns with the format used in
response.writeHead() and request.rawHeaders.
Copy link
Member

@pimterry pimterry left a comment

Choose a reason for hiding this comment

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

There's a couple of tweaks required here, and the linting is still failing for the commit message & markdown. You'll need to look at the failure details there and fix this. For the markdown, you can test this locally with make lint-md.

For the commit message, the issue is that you've added a new commit, but the linting always runs against the first commit in the PR (because that's the commit that typically later appears in the changelog). You'll need to edit that previous commit instead and force push your change to overwrite it.

doc/api/http.md Outdated
@@ -4329,3 +4341,6 @@ A browser-compatible implementation of [`WebSocket`][].
[`writable.uncork()`]: stream.md#writableuncork
[`writable.write()`]: stream.md#writablewritechunk-encoding-callback
[initial delay]: net.md#socketsetkeepaliveenable-initialdelay
[`response.writeHead()`]: https://nodejs.org/api/http.html#responsewriteheadstatuscode-statusmessage-headers
Copy link
Member

Choose a reason for hiding this comment

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

I think you can skip this one - it's already defined above (line 4323)

doc/api/http.md Outdated
@@ -4329,3 +4341,6 @@ A browser-compatible implementation of [`WebSocket`][].
[`writable.uncork()`]: stream.md#writableuncork
[`writable.write()`]: stream.md#writablewritechunk-encoding-callback
[initial delay]: net.md#socketsetkeepaliveenable-initialdelay
[`response.writeHead()`]: https://nodejs.org/api/http.html#responsewriteheadstatuscode-statusmessage-headers
[`request.rawHeaders`]: https://nodejs.org/api/http.html#requestrawheaders
Copy link
Member

Choose a reason for hiding this comment

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

Can you update this so it's not absolute? It should be #requestrawheaders like the other links above. That way it'll work properly locally, or if we ever change the URLs.

- Removed duplicate definition of [`response.writeHead()`] as it already exists earlier in the file.
- Updated [`request.rawHeaders`] link to be relative (#requestrawheaders) instead of using an absolute URL.

These fixes resolve markdown linting issues flagged by the CI and ensure consistency with documentation standards.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. http Issues or PRs related to the http subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

http.request should be documented to receive an headers array
4 participants