415-Fehler „Nicht unterstützter Medientyp“ bei Verwendung von wp_remote_post

Lesezeit: 2 Minuten

Benutzeravatar von camelot
Kamelot

Ich habe versucht, eine POST-Anfrage von WordPress an eine externe API zu senden (einem Benutzer in einem CRM-System ein Tag zuzuweisen). Als ich cURL benutzte, war alles in Ordnung. Hier ist der cURL-Code:

function my_function () {

$body = array ( 'tags' => array ( array (
                        'email' => '[email protected]',
                        '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'); 

Aber dann bin ich auf Nutzung umgestiegen wp_remote_posthabe ich die Antwort „415 – Nicht unterstützter Medientyp“ erhalten.

$body = array ( 'tags' => array (
                            array(
                                'email' => '[email protected]',
                                '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>';

Ich habe viele Modifikationen ausprobiert (assoziatives Array-Format in Schlüssel:Wert-Paar geändert, add AddType zu htaccess Datei…), aber nichts hat funktioniert. Bitte helft mir, ich stecke fest

Benutzeravatar von Sally CJ
Sally C.J

Ausschnitt aus KeyCDN:

EIN 415 Unsupported Media Type Fehler tritt auf, wenn der Ursprungsserver
einen bestimmten Antrag ablehnt da die Ressource in einem Format vorliegt, das vom Server für die nicht unterstützt wird HTTP-Methode
Gebraucht. Dieses Problem mit dem nicht unterstützten Formattyp kann durch die Definition in der Ressource verursacht werden Content-Type oder Content-Encoding
Kopfzeilen.

Und in Ihrem Fall ist der Fehler höchstwahrscheinlich aufgetreten, weil Ihre Remote-Anfrage das Falsche gesendet hat Content-Type Header – standardmäßig application/x-www-form-urlencoded wenn die HTTP-Methode POST ist.

Und ja, Sie haben das Recht aufgenommen Content-Type Wert in Ihrem $headers Reihe. Aber leider in deiner $arg Array, an das Sie übergeben haben wp_remote_post()Sie haben die verwendet falscher Array-Schlüsselheaderwas eigentlich sein sollte headers (beachten Sie das “s”).

Also verwenden headers und nicht headerwie Sie unten sehen können:

$api_url="your API URL";

$body = array(
    'tags' => array(
        array(
            'email' => '[email protected]',
            '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 );

1435500cookie-check415-Fehler „Nicht unterstützter Medientyp“ bei Verwendung von wp_remote_post

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy