question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

custom headers omited in the preflight OPTIONS request

See original GitHub issue

Intended outcome: If I set a custom header using this code:

const httpLink = new HttpLink({
  uri: "https://api.newrelic.com/graphql",
});

const authMiddleware = new ApolloLink((operation, forward) => {
  operation.setContext(({ headers = {} }) => ({
    headers: {
      ...headers,
      "api-key": "123",
    },
  }));

  return forward(operation);
});

const client = new ApolloClient({
  link: concat(authMiddleware, httpLink),
  cache: new InMemoryCache(),
});

I expect the header: "api-key": "123" do be part of the preflight OPTIONS request

Actual outcome: The actual request that apollo-client is fetching is this:

fetch("https://api.newrelic.com/graphql", {
  "headers": {
    "accept": "*/*",
    "accept-language": "en-GB,en-US;q=0.9,en;q=0.8",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "cross-site"
  },
  "referrer": "http://localhost:3000/",
  "referrerPolicy": "strict-origin-when-cross-origin",
  "body": null,
  "method": "OPTIONS",
  "mode": "cors",
  "credentials": "omit"
});

request from the browser (using react-apollo-error-template) The request is missing the header api-key: 123

Screen Shot 2022-10-22 at 8 14 42 pm

How to reproduce the issue: I used the react-apollo-error-template and I created a simple commit: https://github.com/casertap/react-apollo-error-template/commit/7158714ce977f07e5cf8d76fa46851569f0f3c59

The repo is here: https://github.com/casertap/react-apollo-error-template

Versions npx: installed 1 in 1.305s

System: OS: macOS 10.15.7 Binaries: Node: 12.20.0 - ~/.nvm/versions/node/v12.20.0/bin/node Yarn: 1.22.19 - /usr/local/bin/yarn npm: 6.14.8 - ~/.nvm/versions/node/v12.20.0/bin/npm Browsers: Chrome: 106.0.5249.119 Firefox: 106.0 Safari: 15.6.1 npmPackages: @apollo/client: ^3.7.0 => 3.7.0

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jerelmillercommented, Oct 24, 2022

@bignimbus looks like no-cors will omit anything but simple headers.

Prevents the method from being anything other than HEAD, GET or POST, and the headers from being anything other than simple headers.

I’ve tried the above and with no-cors and see that the api-key is omitted in the request. FWIW, I’m not able to successfully call New Relic’s API using plain fetch either, so I’m not so sure this is an issue with Apollo Client.

fetch('https://api.newrelic.com/graphql', {
  method: 'POST',
  headers: {
    "API-key": "xxx",
    'accept': 'application/json',
    'content-type': 'application/json',
  },
  body: JSON.stringify({ query: 'query { requestContext { userId } }' })
});

I actually think this is an issue with New Relic’s API itself. The docs use curl or GraphiQL for the examples, so unfortunately they don’t help much either.

I used to work at New Relic and have emailed someone that I know that works on the team in charge of the API. I’ll post back here when I hear back to see if that API allows CORS requests. More than likely, you’ll need to open an issue with New Relic to see if they will allow CORS requests to allow browser apps to use the API. I’m surprised I didn’t see more of this in my time there!


@casertap FYI, the Access-Control-Allow-Credentials is response header from the server that tells the browser whether to expose the response to JavaScript (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Credentials) so unfortunately setting that header in the request won’t have any effect.

0reactions
jerelmillercommented, Nov 2, 2022

@casertap since this issue looks like its on New Relic’s end, I’m going to go ahead and close this. Feel free to open it back up if you need additional help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do you send a custom header in a CORS preflight ...
The server has a custom header that must be sent along with every request. This custom header therefore makes the request 'not simple'...
Read more >
Preflight request - MDN Web Docs Glossary: Definitions of ...
It is an OPTIONS request, using three HTTP request headers: Access-Control-Request-Method , Access-Control-Request-Headers , and the Origin ...
Read more >
CORS FAQ - cors-errors.info
You should omit Access-Control-Request-Headers if there are no custom headers. ... a preflight OPTIONS request when I'm not setting any custom headers?
Read more >
Custom header not being sent during OPTIONS request. #3464
According to Stackoverflow there is no way to send custom headers during preflight in browsers: This is by design; the purpose of the...
Read more >
Express cors middleware
Enabling CORS Pre-Flight. Certain CORS requests are considered 'complex' and require an initial OPTIONS request (called the “pre-flight request”). An ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found