[wp-trac] [WordPress Trac] #37364: Exception thrown by \WP_Http::request when body contains a JSON string

WordPress Trac noreply at wordpress.org
Thu Jul 14 13:08:42 UTC 2016


#37364: Exception thrown by \WP_Http::request when body contains a JSON string
--------------------------+-----------------------------
 Reporter:  sciamannikoo  |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  HTTP API      |    Version:  trunk
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 This is happening with WP 4.6-beta3-38061.

 Scenario:

 A plugin needs to send requests to an external service, which is expecting
 to receive a JSON string in the request body.

 {{{#!php
 <?php
 $url =
 'http://domain.tld/jobs.json?project_id=1234&accesskey=e2e65819764d93d94624b9e58ea931ea&state=any';

 $context = array (
   'method' => 'GET',
   'body' =>
 '{"project_id":1234,"accesskey":"e2e65819764d93d94624b9e58ea931ea","state":"any","api_version":1.1}',
   'sslverify' => false,
   'timeout' => 60,
   'headers' => 'Content-type: application/json',
 );

 }}}

 Calling `\WP_Http::request( $url, $context )` throws this warning:
 {{{
 PHP Warning:  http_build_query(): Parameter 1 expected to be Array or
 Object.  Incorrect value given in /.../wp-
 includes/Requests/Transport/cURL.php on line 505
 }}}


 The point is that `\WP_Http::request` set the "body" element (which in
 this case, again, is a JSON string) in `$data`.

 Then `\Requests::request` (which expect `$data` to be either an array or
 null) is called.

 However, `\Requests_Transport_cURL::request` (which is called by
 `\Requests::request`) expect `$data` to be either a string or an array.

 Is worth noting that this issue does not happen with WP 4.5 and earlier
 versions.

 Please let me know if you need more details.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/37364>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list