1

I want to POST data using fetch API. To send cookies I use credentials: include. If i POST without any headers cookies are visible in request (but body is empty because of none header). When I add headers like in below cookies are not visible.

const data = {
    testKey: 'abc'
};

fetch(BACKEND_URL + '/coupons', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    credentials: 'include',
     body: JSON.stringify(data)
});

setting headers on server:

app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
  res.setHeader('Access-Control-Allow-Credentials', 'true');
  res.setHeader(
    'Access-Control-Allow-Headers',
    'Origin, Access-Control-Allow-Headers, X-Requested-With, Content-Type, Accept , Authorization'
  );
  res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PATCH,DELETE,OPTIONS');
  next();
});
juliomalves
  • 42,130
  • 20
  • 150
  • 146
robert
  • 41
  • 6

0 Answers0