title: How to send back CSRF Token to server with Axios
body: <p>I have an Express Server with an endpoint that generates the csrf token for the client,</p>
  <p>Now, I tried sending back the token in my axios request as below but I keep getting the usual Forbidden: invalid csrf token error.</p>
  <p>Below is my code:</p>
  <pre><code>static async attachCSRFTokenToHeaders(headers) {
      let csrfTokenRequest = await axios.get(EndPoints.CSRF_TOKEN);
      let csRefToken = csrfTokenRequest.data;
      headers['X-CSRF-TOKEN'] = csRefToken.csrfToken;
  }
  
  static async getRequestHeaders() {
      let headers = {};
      //Possibly add more headers
      await this.attachCSRFTokenToHeaders(headers); //Attach the csrf token to the headers of each request
      return headers;
  }
  
  
  static async logInManually(email, password, cb) {
      let requestBody = { email, password};
      axios.post(EndPoints.SIGN_IN, requestBody, {
          headers: await this.getRequestHeaders() //Attach the headers here
      }).then((response) =&gt; {
          cb(HttpSuccessDataHandler.getSuccessResponseData(response), null);
      }).catch((e) =&gt; {
          cb(null, HttpErrorHandler.spitHttpErrorMsg(e));
      });
  }
  </code></pre>
  <p>But the server still keeps throwing the usual:</p>
  <blockquote>
  <p>ForbiddenError: invalid csrf token</p>
  </blockquote>
  <p>Here is a snippet into my server setup</p>
  <pre><code>const csrf = require('csurf');
  const cookieParser = require('cookie-parser');
  const session = require('express-session');
  
  ....
  
  initMiddleWare() {
          app.use(express.static('./static'));
          app.use(express.json());
          app.use(express.urlencoded({ extended: true }));
          app.use(cookieParser())
          app.use(session({
              secret: Constants.SESSIONS_SECRET,
              resave: false,
              saveUninitialized: false
          }));
          app.use(busboy({
              highWaterMark: 2 * 1024 * 1024,
              limits: {
                  fileSize: maxFileSize,
              }
          }));
          app.use(csrf({ cookie: true }))
      }
  
  
   //Then somewhere in my routes, here is the route that provides the csrf token
  .....
     app.get(Routes.CSRF_TOKEN, function (req, res) {
          res.send({ csrfToken: req.csrfToken() });
         });
  ....
  </code></pre>