Skip to content
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

Node's fetch implementation does not allow changing headers on request object #1433

Closed
evert opened this issue May 11, 2022 · 4 comments
Closed

Comments

@evert
Copy link

evert commented May 11, 2022

Version

v18.1.0

Platform

Linux evertbook6 5.15.0-27-generic nodejs/node#28-Ubuntu SMP Thu Apr 14 04:55:28 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

Node will lose headers that were added to the request after constructing the Request object, but_only_ if a custom Headers object was also passed in. This reproduces the issue

const headers = new Headers();
const request = new Request('http://localhost:8080', { headers });
request.headers.set('X-Foo', 'bar');

await fetch(request);

This does not happen when a headers object was not passed in:

const request = new Request('http://localhost:8080');
request.headers.set('X-Foo', 'bar');

await fetch(request);

Oof this took a lot of experimentation to get to :S

I think this was introduced with Node 18.1.0, because I have a feeling my my tests passed with 18.0.0

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior?

The following headers on the server:

{
  host: 'localhost:8080',
  connection: 'keep-alive',
  'x-foo': 'bar',
  accept: '*/*',
  'accept-language': '*',
  'sec-fetch-mode': 'cors',
  'user-agent': 'undici',
  'accept-encoding': 'gzip, deflate'
}

What do you see instead?

{
  host: 'localhost:8080',
  connection: 'keep-alive',
  accept: '*/*',
  'accept-language': '*',
  'sec-fetch-mode': 'cors',
  'user-agent': 'undici',
  'accept-encoding': 'gzip, deflate'
}

@jasnell

@targos targos transferred this issue from nodejs/node May 11, 2022
@ronag
Copy link
Member

ronag commented May 11, 2022

I think this was fixed in #1401

@evert
Copy link
Author

evert commented May 11, 2022

I'm not fully certain if #1401 tests for the the exact same issue. One thing that's at least different is that I'm changing a header after constructing the Request object. I don't think there's a test there that covers that case, but it does seem related to very possible that this is also fixed.

@mcollina
Copy link
Member

Could you verify if this still applies to undici v5.2.0?

@evert
Copy link
Author

evert commented May 11, 2022

I imported Undici in my project directly, overwrite global.fetch() and family and can confirm it's fixed!

@evert evert closed this as completed May 11, 2022
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

No branches or pull requests

3 participants