title: How to pass Header JWT Token with Axios & React?
body: <p>I make web application with React, Express, MongoDB.</p>
  
  <p>And, I want to pass jwt token with header.</p>
  
  <p>But, I pass it, get 401 error (Unauthorized).</p>
  
  <p>In login actions.js :</p>
  
  <pre><code>export function login(username, password) {
  return function(dispatch) {
    axios
    .post(`${API_URL}/auth/login`, { username, password })
    .then(res =&gt; {
      dispatch(loginSuccess(res.data, username));
      const token = res.data.token;
      axios.defaults.headers.common["Authorization"] = token;
      history.push("/");
    })
    .catch(err =&gt; {
      if (err.response.status === 401) {
        dispatch(loginFailure(err));
      }
    });
   };
  }
  </code></pre>
  
  <p>And, In my post.js in server :</p>
  
  <pre><code>getToken = function(headers) {
    if (headers &amp;&amp; headers.authorization) {
      var parted = headers.authorization.split(" ");
        if (parted.length === 2) {
         return parted[1];
        } else {
         return null;
        }
      } else {
       return null;
      }
   };
  ...
  // Save Post
  router.post("/", passport.authenticate("jwt", { session: false }), 
   function(
    req,
    res,
    next
    ) {
   var token = getToken(req.headers);
   if (token) {
     Post.create(req.body, function(err, post) {
       if (err) return next(err);
        res.json(post);
       });
     } else {
      return res.status(403).send({ success: false, msg: "Unauthorized." });
     }
  });
  </code></pre>
  
  <p>How I do fix it?
   + Login is success</p>