Visitar URL original
fix: send Host header as the first header by singhvishalkr · Pull Request #1888 · node-fetch/node-fetch · GitHub
Skip to content

fix: send Host header as the first header - #1888

Open
singhvishalkr wants to merge 1 commit into
node-fetch:mainfrom
singhvishalkr:fix/1570-host-header-first
Open

singhvishalkr wants to merge 1 commit into
node-fetch:mainfrom
singhvishalkr:fix/1570-host-header-first

Conversation

@singhvishalkr

Copy link
Copy Markdown

When making requests, node-fetch currently sends the Host header as the last header if the user did not provide one. Browsers and other HTTP libraries (like curl) send Host as the first header.

This PR updates getNodeRequestOptions to:

  1. Automatically set the Host header from the URL if not already present
  2. Ensure Host is always the first header in the outgoing request

The fix reconstructs the headers object with Host first, then all other headers in their original order.

I also added two tests to verify the behavior.

Closes #1570

Currently node-fetch sends the Host header as the last header when it is automatically added. Browsers and other HTTP libraries send Host as the first header.

This change prepends the Host header to match browser behavior. If the user provides a custom Host header, it is still placed first.

Fixes node-fetch#1570
@singhvishalkr
singhvishalkr force-pushed the fix/1570-host-header-first branch from f973c7e to aa894cb Compare May 25, 2026 17:13
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.

Send Host header as the first header

1 participant