title: PHP cURL POST returns a 415 - Unsupported Media Type
body: <p>I've got a simple PHP script that sends an HTTP POST request via cURL, and expects a json string in response (would have loved to use an existing library like pecl_http/HTTPRequest for this, but can't). The call consistently fails with a 415 error - Unsupported Media Type.  I think I'm not configuring cURL correctly, but after much searching, I can't find out what I'm doing wrong.  Here's some code:</p>
  
  <pre><code>class URLRequest
  {
      public $url;
      public $headers;
      public $params;
      public $body;
      public $expectedFormat;
      public $method;
  
      public function URLRequest($aUrl, array $aHeaders, array $aParams, $aFormat = "json", $isPost = false, $aBody = "+")
      {
          $this-&gt;url = $aUrl;
          $this-&gt;headers = $aHeaders;
          $this-&gt;params = $aParams;
          $this-&gt;expectedFormat = $aFormat;
          $this-&gt;method = ($isPost ? "POST" : "GET");
          $this-&gt;body = $aBody;
  
      }
  
      public function exec()
      {
  
          $queryStr = "?";
          foreach($this-&gt;params as $key=&gt;$val)
              $queryStr .= $key . "=" . $val . "&amp;";
  
          //trim the last '&amp;'
          $queryStr = rtrim($queryStr, "&amp;");
  
          $url = $this-&gt;url . $queryStr;
  
          $request = curl_init();
          curl_setopt($request, CURLOPT_URL, $url);
          curl_setopt($request, CURLOPT_HEADER, 1);
          curl_setopt($request, CURLOPT_HTTPHEADER, $this-&gt;headers);
          curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
          //curl_setopt($request, CURLOPT_SSL_VERIFYPEER, false);
  
          if($this-&gt;method == "POST")
          {
              curl_setopt($request, CURLOPT_POST, 1);
              curl_setopt($request, CURLOPT_POSTFIELDS, $this-&gt;body);
  
              //this prevents an additions code 100 from getting returned
              //found it in some forum - seems kind of hacky
              curl_setopt($request, CURLOPT_HTTPHEADER, array("Expect:"));
          }
  
          $response = curl_exec($request);
          curl_close($request);
  
          preg_match("%(?&lt;=HTTP/[0-9]\.[0-9] )[0-9]+%", $response, $code);
  
          $resp = "";
          if($this-&gt;expectedFormat == "json")
          {
              //parse response
          }
          elseif($this-&gt;expectedFormat == "xml")
          {
              //parse response
          }
  
          return $resp;
  
      }
  }
  
  
  $url = "http://mydomain.com/myrestcall";
  
  $query = array( "arg_1" =&gt;      "test001",
                  "arg_2" =&gt;      "test002",
                  "arg_3" =&gt;      "test003");
  
  $headers = array(    "Accept-Encoding" =&gt;    "gzip",
                      "Content-Type" =&gt;       "application/json",
                      "Accept" =&gt;             "application/json",
                      "custom_header_1" =&gt;    "test011",
                      "custom_header_2" =&gt;    "test012",
                      "custom_header_3" =&gt;    "test013");
  
  $body = array(  "body_arg_1" =&gt;      "test021",
                  "body_arg_2" =&gt;     array("test022", "test023"), 
                  "body_arg_3" =&gt;     "test024"); 
  
  
  $request = new URLRequest($url, $headers, $query, "json", true, $body);
  
  $response = $request-&gt;exec();
  </code></pre>
  
  <p>...and the response:</p>
  
  <pre><code>HTTP/1.1 415 Unsupported Media Type
  Server: Apache-Coyote/1.1
  X-Powered-By: Servlet 2.5; JBoss-5.0/JBossWeb-2.1
  Content-Type: text/html;charset=utf-8
  Content-Length: 1047
  Date: Mon, 18 Jun 2012 16:30:44 GMT
  
  &lt;html&gt;&lt;head&gt;&lt;title&gt;JBoss Web/2.1.3.GA - Error report&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;HTTP Status 415 - &lt;/h1&gt;&lt;p&gt;&lt;b&gt;type&lt;/b&gt; Status report&lt;/p&gt;&lt;p&gt;&lt;b&gt;message&lt;/b&gt; &lt;u&gt;&lt;/u&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;description&lt;/b&gt; &lt;u&gt;The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().&lt;/u&gt;&lt;/p&gt;&lt;h3&gt;JBoss Web/2.1.3.GA&lt;/h3&gt;&lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>Any insights or ideas?</p>
  
  <p>Thanks in advance!</p>