WordPress / wordpress-playground

Run WordPress in the browser via WebAssembly PHP
https://w.org/playground/
GNU General Public License v2.0
1.65k stars 263 forks source link

wp_remote_post Content-Type header not respected #1428

Open key88sf opened 6 months ago

key88sf commented 6 months ago

My plugin uses wp_remote_post with a Content-Type: application/json header (see below for code sample). While it works fine in regular WordPress installs, when this code runs in Playground, the Content-Type header is not respected, and instead gets sent as application/x-www-form-urlencoded. This causes my wp_remote_post() call to fail since the server returns an error.

Example of wp_remote_post() call with content-type header:

    $response = wp_remote_post(
      $this->base_url . '/images',
      array(
        'headers'       => array(
          'Content-Type'  => 'application/json',  /* THIS IS NOT BEING RESPECTED by Playground */
          'X-Api-Key'     => $this->api_key
        ),
        'body'          => wp_json_encode( $body )
      )
    );

I confirmed this same exact request to the server endpoint with the proper content-type header works as expected.

The Chrome network tab confirms playground is sending Content-Type: application/x-www-form-urlencoded: Screenshot 2024-05-18 at 1 12 44 PM

My blueprint file DOES include networking, etc:

  "phpExtensionBundles": [
    "kitchen-sink"
  ],
  "features": {
    "networking": true
  },

Any ideas if it is possible to fix this?

adamziel commented 6 months ago

Let’s:

Also, I’m re-categorizing this as a crash since it’s an error in a basic WordPress feature

danielpost commented 12 hours ago

Got the same issue. This currently makes my plugin not usable with Playground so would be great to get fixed. Is there a potential workaround in the meantime?