title: React Axios and Fetch POST method not working with C# WebAPI token request
body: <p>I am struggling to get a C# WebAPI auth token using react. I have a Postman request that works correctly; it gives me my token as expected:</p>
  
  <p><a href="https://i.stack.imgur.com/TBLvD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TBLvD.png" alt="enter image description here"></a></p>
  
  <p>I have tried recreating this request using both Axios and Fetch. The requests were as follows:</p>
  
  <pre><code>Attempt 1:
  fetch('http://localhost:56765/token', {
      method: 'POST',
      headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
      },
      body: JSON.stringify({ username: 'test@test.com', password: 'Test123!', grant_type: 'password' })
  })
  
  Attempt 2:
  axios('http://localhost:56765/token', {
      method: 'POST',
      headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: JSON.stringify({ username: 'test@test.com', password: 'Test123!', grant_type: 'password' })
  })
  
  Attempt 3:
  const data = JSON.stringify({ username: 'test@test.com', password: 'Test123!', grant_type: 'password' });
  const headers = { 'Content-Type': 'application/x-www-form-urlencoded', };
  axios.post('http://localhost:56765/token', data, { headers })
  </code></pre>
  
  <p>When I make the request, the response is:</p>
  
  <pre><code>400 Bad Request
  {"error":"unsupported_grant_type"}
  </code></pre>
  
  <p>Picture example:
  <a href="https://i.stack.imgur.com/NcjKq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NcjKq.png" alt="enter image description here"></a></p>
  
  <p>Does anyone know where I am going wrong?</p>
  
  <p>Edit:</p>
  
  <p>The code that worked for me was installing <code>qs</code> then:</p>
  
  <pre><code>var qs = require('qs');
  
  const data = qs.stringify({ username: 'test@test.com', password: 'Test123!', grant_type: 'password' });
  axios.post('http://localhost:56765/token', data)
  </code></pre>
  
  <p>I was able to completely omit the header.</p>
  
  <p>Many thanks to Brub for answering on Xmas day!</p>