Headers are not passed when using cross-fetch
See original GitHub issueIntended outcome:
Passing custom headers along with my requests when using ApolloClient
.
Actual outcome: When using cross-fetch the headers are not passed as expected.
How to reproduce the issue:
Consider a very simple client running on node.js:
const {
ApolloClient,
HttpLink,
InMemoryCache,
gql,
} = require("@apollo/client/core");
const { fetch } = require("cross-fetch");
const client = new ApolloClient({
link: new HttpLink({ uri: "https://hookb.in/xYjbyaxeOLh0nNxxnWpY", fetch }),
cache: new InMemoryCache(),
headers: { hello: "world" },
});
client
.query({
query: gql`
query {
fart
}
`,
})
.then(console.log);
It’s set up to hit a temporary hookbin endpoint and pass along a hello
HTTP header with value world
. Hookbin receives the request, but there’s no custom headers received at all:
Versions
System:
OS: macOS 11.6
Binaries:
Node: 15.14.0 - ~/.nvm/versions/node/v15.14.0/bin/node
Yarn: 1.22.17 - /usr/local/bin/yarn
npm: 7.7.6 - ~/.nvm/versions/node/v15.14.0/bin/npm
Browsers:
Chrome: 95.0.4638.54
Firefox: 93.0
Safari: 15.0
npmPackages:
@apollo/client: ^3.4.16 => 3.4.16
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
fetch() does not send headers? - Stack Overflow
To send a cross-origin request with headers like Authorization and X-My-Custom-Header , you have to drop the no-cors mode and support ...
Read more >Using the Fetch API - MDN Web Docs
Instead, as soon as the server responds with headers, the Promise will resolve normally (with the ok property of the response set to...
Read more >Fetch API - The Modern JavaScript Tutorial
If we send a fetch , then by default it always sends the Referer header with the full url of our page (except...
Read more >node-fetch - npm
Start using node-fetch in your project by running `npm i node-fetch`. ... extracted and passed by manipulating request and response headers.
Read more >How do I send requests using JavaScript Fetch API? - ReqBin
By default, a Fetch API request does not contain user credentials such as cookies and HTTP authentication headers such as a bearer token...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The above solution is what we used and it works as expected.
@samuela I’m curious, if you move the
headers
into theHttpLink
options, does it work? It looks like we only do something with thatheaders
value passed directly toApolloClient
when you provide no link. This would explain why you’re not seeing headers. Thatheaders
value passed directly to the client is mostly a convenience if you don’t pass alink
to it.Try this and see if you have better luck 🤞 :