PHP cURL POST returns a 415 - Unsupported Media Type
Problem solved! Here's the issue:
Sending an associative-array of headers DOES NOT WORK with cURL. There are several forums scattered around that show examples using an associative array for headers. DON'T DO IT!
The correct way (which is also scattered around the internets, but that I'm too dense to have noticed) is to construct your header key/value pairs as strings, and pass a standard array of these strings when setting the CURLOPT_HTTPHEADER option.
So in summary,
WRONG:
$headers = array( "Accept-Encoding" => "gzip",
"Content-Type" => "application/json",
"custom_header_1" => "test011",
"custom_header_2" => "test012",
"custom_header_3" => "test013");
RIGHT:$headers = array( "Accept-Encoding: gzip",
"Content-Type: application/json",
"custom_header_1: test011",
"custom_header_2: test012",
"custom_header_3: test013");
I hope this comes in handy to some other noble doofus down the road before they waste as much time debugging as I did.If I had to guess, I would assume that the same rule applies to the POST body key/value pairs as well, which is why @drew010 's comment about using http_build_query()
or json_encode()
to stringify your message body is a great idea as well.
Thanks to everyone for your very useful comments, and for you time and consideration. In the end, a side by side comparison of the http traffic (captured via Wireshark) revealed the issue.
Thanks!
Curl : HTTP 415 Unsupported Media Type
Your missing the post data. I'm not sure of your exact query but an example query is in this code:
<?php
function getContentCurl($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"activeFiltre":true,"motsCles":"","fonctions":[],"lieux":[],"pointGeolocDeReference":{},"secteursActivite":[],"typesContrat":[],"typesConvention":[],"niveauxExperience":[],"sorts":[{"type":"DATE","direction":"DESCENDING"}],"pagination":{"startIndex":0,"range":20},"typeClient":"CADRE"}');
return $result = curl_exec($ch);
}
$url = "https://cadres.apec.fr/cms/webservices/rechercheOffre/ids";
$content = getContentCurl($url);
var_dump($content);
Outputs: string(4083) "{"totalCount":78073,"offreFilters":[{"offreFiltering":"CONTRACT_TYPE_FILTERING","offreFilterItems":[{"key":101888,"count":69550},{"key":20053,"count":120},{"key":101930,"count":2144},{"key":101887,"count":6259}]},{"offreFiltering":"CONTRACT_DURATION_FILTERING","offreFilterItems":[{"key":143691,"count":4981},{"key":143692,"count":2562},{"key":143693,"count":976}]},{"offreFiltering":"COMPANY_TYPE_FILTERING","offreFilterItems":[{"key":143684,"count":35345},{"key":143685,"count":18112},{"key":143686,"count":13180},{"key":143687,"count":11436}]},{"offreFiltering":"POSITION_STATUS_FILTERING","offreFilterItems":[{"key":143688,"count":65264},{"key":143689,"count":1629},{"key":143690,"count":11180}]},{"offreFiltering":"EXPERIENCE_FILTERING","offreFilterItems":[{"key":101881,"count":36267},{"key":20043,"count":45182},{"key":20044,"count":11797},{"key":20045,"count":11317}]},{"offreFiltering":"GEOLOCALISABLE_FILTERING","offreFilterItems":[{"key":0,"count":6088},{"key":1,"count":71985}]},{"offreFiltering":"JOB_CODE_FILTERING","offreFilterItems":[{"key":101829,"count":1360},{"key":101828,"count":19240},{"key":101831,"count":11451},{"key":101830,"count":1336},{"key":101833,"count":17141},{"key":101832,"count":10470},{"key":101835,"count":4190},{"key":101834,"count":7131},{"key":101837,"count":11231},{"key":101836,"count":1463}]},{"offreFiltering":"LOCATION_FILTERING","offreFilterItems":[{"key":102099,"count":731},{"key":99700,"count":411},{"key":799,"count":77342}]},{"offreFiltering":"NAF_CODE_FILTERING","offreFilterItems":[{"key":101761,"count":1083},{"key":101760,"count":3773},{"key":101763,"count":3607},{"key":101762,"count":11726},{"key":101765,"count":385},{"key":101764,"count":2123},{"key":101767,"count":1134},{"key":101766,"count":1757},{"key":101769,"count":621},{"key":101768,"count":169},{"key":101771,"count":332},{"key":101770,"count":1241},{"key":101773,"count":9665},{"key":101772,"count":9572},{"key":101775,"count":695},{"key":101774,"count":2410},{"key":101777,"count":1597},{"key":101776,"count":1292},{"key":101779,"count":1308},{"key":101778,"count":336},{"key":101780,"count":158},{"key":101753,"count":11418},{"key":101752,"count":1529},{"key":101755,"count":1026},{"key":101754,"count":2418},{"key":101757,"count":4055},{"key":101756,"count":1413},{"key":101759,"count":959},{"key":101758,"count":271}]},{"offreFiltering":"NIVEAU_ETUDE"},{"offreFiltering":"DUREE_STAGE","offreFilterItems":[{"key":599765,"count":381},{"key":599766,"count":233},{"key":599767,"count":710},{"key":599768,"count":441},{"key":599769,"count":280},{"key":599770,"count":2936},{"key":599771,"count":3538}]},{"offreFiltering":"DATE_PRISE_POSTE","offreFilterItems":[{"key":599777,"count":75435},{"key":599778,"count":2209},{"key":599779,"count":429}]},{"offreFiltering":"PERIOD_FILTERING","offreFilterItems":[{"key":101850,"count":11054},{"key":101851,"count":32259},{"key":101852,"count":78073},{"key":101853,"count":78073}]},{"offreFiltering":"WAGE_FILTERING","offreFilterItems":[{"key":597179,"count":7147},{"key":599738,"count":38996},{"key":101840,"count":56141},{"key":101841,"count":25697},{"key":101842,"count":6213},{"key":101843,"count":1408}]}],"resultats":[{"@uriOffre":"offre?numeroOffre=163035045W"},{"@uriOffre":"offre?numeroOffre=163035043W"},{"@uriOffre":"offre?numeroOffre=163035044W"},{"@uriOffre":"offre?numeroOffre=162918884W"},{"@uriOffre":"offre?numeroOffre=162913997W"},{"@uriOffre":"offre?numeroOffre=163035042W"},{"@uriOffre":"offre?numeroOffre=162930465W"},{"@uriOffre":"offre?numeroOffre=162926697W"},{"@uriOffre":"offre?numeroOffre=163035041W"},{"@uriOffre":"offre?numeroOffre=163035040W"},{"@uriOffre":"offre?numeroOffre=163035039W"},{"@uriOffre":"offre?numeroOffre=163035036W"},{"@uriOffre":"offre?numeroOffre=163035032W"},{"@uriOffre":"offre?numeroOffre=163035031W"},{"@uriOffre":"offre?numeroOffre=163035030W"},{"@uriOffre":"offre?numeroOffre=163035029W"},{"@uriOffre":"offre?numeroOffre=163035027W"},{"@uriOffre":"offre?numeroOffre=163035026W"},{"@uriOffre":"offre?numeroOffre=163035022W"},{"@uriOffre":"offre?numeroOffre=163035020W"}]}"
Got 415 error Unsupported Media Type when use wp_remote_post
Excerpt from KeyCDN:
AAnd in your case, the error happened most likely because your remote request sent the wrong415 Unsupported Media Type
error occurs when the origin server
refuses a particular request since the resource is in a format that is not supported by the server for the HTTP
method
used. This unsupported format type issue can be caused by what is
defined in the resource'sContent-Type
orContent-Encoding
headers.
Content-Type
header — which defaults to application/x-www-form-urlencoded
when the HTTP method is POST.And yes, you did include the right Content-Type
value in your $headers
array. But unfortunately in your $arg
array which you passed to wp_remote_post()
, you used the wrong array key — header
, which should actually be headers
(note the "s").
So use headers
and not header
, just as you can see below:
$api_url = 'your API URL';
$body = array(
'tags' => array(
array(
'email' => 'xxx@gmail.com',
'tag' => 'Customer5',
),
),
);
$headers = array(
'Content-Type' => 'application/json',
'User-Agent' => 'Your App Name (www.yourapp.com)',
'Authorization' => 'Basic xxxxxx',
);
$arg = array(
'headers' => $headers, // good
// 'header' => $headers, // bad; i.e. wrong array key ('header')
'body' => json_encode( $body ),
// 'method' can be omitted since you're using wp_remote_post()
'method' => 'POST',
'sslverify' => false,
);
$request = wp_remote_post( $api_url, $arg );
// ..if the response still isn't good, what's the output of this:
var_dump( $request );
415 Unsupported Media Type in PostMan but not in curl
I can see two main problems:
- In body request you are sending an invalid Json, because you add -d before and simple quote at the end (eliminate both).
- You don't set header/content type right. To achieve it click in Text (on the right binary button) and set in JSON (aplication/json), with that postman add header for request.
PD: I tried make your request, but I never receive an 200 status code, most requests were timeout (I don't know if you run that server locally or have a special proxy configuration).
Related Topics
PHP Class Not Found But It's Included
How to Refresh Select2 Dropdown Menu After Ajax Loading Different Content
Iterate in Reverse Through an Array with PHP - Spl Solution
Setting PHP Enviromental Variable While Running Command Line Script
PHP - Good Cronjob/Crontab/Cron Tutorial or Book
Laravel Catch Eloquent "Unique" Field Error
How to Send HTML Mails Using Pear Mail
Ssl Alternative - Encrypt Password with JavaScript Submit to PHP to Decrypt
Login Event Handling in Laravel 5
Remove Extra Spaces But Not Space Between Two Words
Phpmailer - Mails Going Straight to Spam
Adding an Admin Page on Opencart Version 2
Error Starting Userland Proxy: Listen Tcp 0.0.0.0:3306: Bind: Address Already in Use