Send Session-Cookies with Axios

2019-10-03

It sets the following for sending session-cookies to server with axios.

    axios.get('url',
      {
        withCredentials: true
      }
    )

Then CORS can enabled with the following on server side.(use cors

      import * as express from "express";
      const cors = require('cors')
      const app = express()
      app.use(cors({credentials: true, origin: true}));

This is all the browser session information are sent for server with axios.

Set Cookie with Axios in Server-Side

There are the case that are requesting WebAPI on server. And sometimes it is necessary to send cookies from browser in server side. In that case, “withCredentials" is not working. Rightly, because that is a connection from server.

The way of solving a problem is setting cookies as follows.

    auth.get('/', function(req, res, next) {
          axios.get('url',
            headers: {
                Cookie: req.headers.cookie
            }
          )
    });

It just set axios header with Express request header.