title: PHP Curl sent wrong Content-Type in post request
body: <p>I am trying to sent a Post Request to VendHQ Api using oauth2 auth method. I've the correct code, client_id, client_secret etc as It work fine in Postman but when I try to sent the same data using PHP curl, I get the error: </p>
  
  <p><strong>Error</strong></p>
  
  <pre><code>'error' =&gt; string 'invalid_request' (length=15)
  'error_description' =&gt; string 'The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed. Check the "grant_type" parameter.' (length=179)
  </code></pre>
  
  <p>This is the documentation of <a href="https://developers.vendhq.com/documentation/oauth.html#step-1-requesting-authorisation-code" rel="nofollow">Requesting Access Token</a> and this is my code which is trying to get the access token.</p>
  
  <p><strong>PHP Code:</strong></p>
  
  <pre><code>$prefix     = $vend[0]['domain_prefix'];
  $request_url = 'https://'.$prefix.'.vendhq.com/api/1.0/token';
  
  $body['code']           = $vend[0]['code'];;
  $body['client_id']      = $vend[0]['app_id'];;
  $body['client_secret']  = $vend[0]['app_secret'];;
  $body['grant_type']     = 'authorization_code';
  $body['redirect_uri']   = $vend[0]['redirect_uri'];;
  
  $response = $this-&gt;invoke($request_url, 'POST', $body);
  </code></pre>
  
  <p><strong>Invoke function</strong></p>
  
  <pre><code>private function invoke($url, $method, $data = null)
  {
  
      $ch = curl_init($url);
  
      if($method=='POST'){
  
  
          if(isset($data)){
              $data_string = json_encode($data);
          }
  
  
          curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
          curl_setopt($ch, CURLOPT_POST, true);
          curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  
          $headers = array();
          $headers[] = "Content-Type: application/x-www-form-urlencoded;charset=UTF-8";
          $headers[] = 'Content-Length: '.strlen($data_string);
  
          echo '&lt;br&gt;Curl Headers';
          var_dump($headers);
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  
      }//END POST
  
  
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  
      $json = curl_exec($ch);
  
      $info = curl_getinfo($ch);
      echo '&lt;pre&gt;Curl Info&lt;br&gt;';
      var_dump($info);
      echo '&lt;/pre&gt;';
  
      curl_close($ch);
  
      $json_output = json_decode($json, true);
  
      return $json_output;
  
  }//end function
  </code></pre>
  
  <p>I believe I am sending everything fine but the curl is sending but from curl info I get this</p>
  
  <pre><code>'content_type' =&gt; string 'application/json; charset=UTF-8' (length=31)
  </code></pre>
  
  <p>But VendAPI <a href="https://developers.vendhq.com/documentation/oauth.html#step-1-requesting-authorisation-code" rel="nofollow">Documentation</a> says to send post data as <strong>"application/x-www-form-urlencoded"</strong>.</p>
  
  <blockquote>
    <p><strong>NOTE</strong> Those parameters should be sent as “application/x-www-form-urlencoded”
    encoded body of a POST request</p>
  </blockquote>
  
  <p>What I am doing wrong?</p>