I am writing a WordPress plugin that uses WP_HTTP for making an API call.
The code is as below:
$request = new WP_Http;
$headers = array(
'Content-Type: application/x-www-form-urlencoded', // required
'accesskey: abcdefghijklmnopqrstuvwx', // required - replace with your own
'outputtype: json' // optional - overrides the preferences in our API control page
);
$response = $request->request('https://api.abcd.com/clients/listmethods', array( 'sslverify' => false, 'headers' => $headers ));
But I am getting the response as “406 Not Acceptable”.
When I tried using cURL for the above request the request was successful.
The 406 error indicates the web service probably isn’t recognizing your Content-Type header, so it doesn’t know what format it’s responding to. Your
$headers
variable should be an associative array, like this:or a string that looks like a raw header (including newlines), like this:
The WP_Http class will convert a raw header string into an associative array, so you’re nominally better off just passing it an array in the first place.
I know this answer is late but sure will help others.
WP_Http function should be used like below
Please also refer to http://planetozh.com/blog/2009/08/how-to-make-http-requests-with-wordpress/ for more info.