4

I tried to send a POST request from WordPress to an external API (assign a tag to an user in a CRM system). When I used cURL, everything was OK. Here is the cURL code:

function my_function () {

$body = array ( 'tags' => array ( array (
                        'email' => 'xxx@gmail.com',
                        'tag' => 'Customer5'
                        ))
);

$curl = curl_init();

curl_setopt_array($curl, array(

  CURLOPT_URL => "$api_url",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => json_encode($body, true),
  CURLOPT_HTTPHEADER => array(
    "Content-Type: application/json",
    "User-Agent: Your App Name (www.yourapp.com)",
    "Authorization: Basic xxxxxx"
  ),
));

$response = curl_exec($curl);

curl_close($curl);

var_dump($response);

}

add_action( 'init', 'my_function'); 

But then I switched to use wp_remote_post, I got a "415 - Unsupported Media Type" response.

$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',
);

$request = wp_remote_post('$api_url', $arg );

$arg = array (
    'header' => $headers,
    'body' => json_encode($body, true),
    'method' => 'POST',
    'sslverify' => false,
);

echo '<pre>';
print_r($request);
echo '</pre>';

I tried a lot of modifications (changed associative array format to key:value pair, add AddType to htaccess file...), but nothing worked. Please help, I'm stuck

1 Answer 1

3
+100

Excerpt from KeyCDN:

A 415 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's Content-Type or Content-Encoding headers.

And in your case, the error happened most likely because your remote request sent the wrong 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 keyheader, 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 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.