title: Keycloak login with API
body: <p>i have a html page with javascript where i want to auto-login the user. I have the following code:</p>
  
  <pre><code>var url = "http://localhost:8180/auth/realms/Myrealm/protocol/openid-connect/token";
  
  const response = await fetch(url, {
      mode: 'no-cors',
      method: "POST",
      body: JSON.stringify({
          "client_id":"myclientid",
          "username":"admin",
          "password":"123",
          "grant_type":"password"
      }),
      headers:{
          //"Content-type":"application/x-www-form-urlencoded", 
          "Content-Type": "application/json"
      }
  })
  </code></pre>
  
  <p>On the keycloak server i added <strong>Web Origins '*'</strong>. I get the following error :</p>
  
  <blockquote>
    <p>POST <a href="http://localhost:8180/auth/realms/Myrealm/protocol/openid-connect/token" rel="nofollow noreferrer">http://localhost:8180/auth/realms/Myrealm/protocol/openid-connect/token</a> 400 (Bad Request)</p>
  </blockquote>
  
  <p>I dont know why it is not working. When i use the terminal it works fine:</p>
  
  <pre><code>curl -i -d "client_id=myclientid" -d "username=admin" -d "password=123" -d "grant_type=password" http://localhost:8180/auth/realms/Myrealm/protocol/openid-connect/token
  </code></pre>
  
  <p>(keycloak version 4.8.3)</p>
  
  <hr>
  
  <h2>Update</h2>
  
  <pre><code>const response = await fetch(url, {
      method: "POST",
      body: 'client_id=myclientid&amp;password=123&amp;username=admin&amp;grant_type=password',
      headers:{
          "Content-type":"application/x-www-form-urlencoded"
      }
  })
  </code></pre>
  
  <p>and i get the following response:</p>
  
  <p><a href="https://i.stack.imgur.com/XG3xC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XG3xC.png" alt="response"></a></p>