2

I am calling a fetch request akin to this.

fetch('https://api-endpoint.com/api',
        {
            method: "POST",
            headers: new Headers({
                'custom-header': 'custom header value'
            })
        }
     )
    .then(res=>{
        /* code */
    })
    .catch(err=>{
        /* code */
    })

But it seems that the header is not being sent to the server. The server runs on Node.js, and I am attempting to reach it with React-Native.

I have allowed "access-control-allow-origin": "*" on the server, but to no avail.

I can also reach other endpoints on the server that don't require any headers.

And lastly, I have set the headers with both new Headers() and as an object.

What exactly am I missing to allow the headers to be sent? Is there a way to see exactly what is going on with my request in react-native?

It works in postman just fine.

EDIT:

I am using the cors middleware in my server.

app.use(cors())

appConfig.init(app);
3
  • You don't need Headers constructor with fetch as object that you tried works just fine: headers : { custom-header : 'custom header value' } Commented Oct 20, 2019 at 11:59
  • can you provide your server-side nodejs code? Commented Oct 20, 2019 at 12:26
  • Did you look at my answer? Commented Oct 20, 2019 at 17:54

2 Answers 2

1

Can you add these lines before using routes and try?

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");

  res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");

  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept, Authorization, custom-header"
  );

  res.header("Access-Control-Expose-Headers", "custom-header");

  next();
});

And if you are using express cors middleware, you can add allowedHeaders and exposedHeaders options.

https://github.com/expressjs/cors#configuration-options

note if these configuration replaces the default headers, you may need to add the default headers to the options.

app.use(
  cors({
    exposedHeaders: ["custom-header"],
    allowedHeaders: ["custom-header"]
  })
);

Lastly you had better to use fetch api like this to send headers:

fetch('https://api-endpoint.com/api',
  {
    method: "POST",
    headers: {
      "custom-header": "custom header value"
    }
  }
)
  .then(res => {
    /* code */
  })
  .catch(err => {
    /* code */
  })

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

Sign up to request clarification or add additional context in comments.

3 Comments

I am using the cors middleware package, so all I did was set app.use(cors()) which sets few default options.
@BrandonArmand did you add your custom header in the allowedHeaders and exposedHeaders in cors options?
I figured it out before you got back to me. Thank you, the allowedHeaders: ["custom-header"] got it working.
0

Be sure,you added the header in:

res.header( "Access-Control-Allow-Headers", "Your_OWN_header,...." );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.