[wp-trac] [WordPress Trac] #40614: REST API: String argument for rest_do_request/rest_ensure_request does not work as expected.

WordPress Trac noreply at wordpress.org
Sun Apr 30 22:07:14 UTC 2017


#40614: REST API: String argument for rest_do_request/rest_ensure_request does not
work as expected.
-------------------------------+-----------------------------
 Reporter:  TimothyBlynJacobs  |      Owner:
     Type:  defect (bug)       |     Status:  new
 Priority:  normal             |  Milestone:  Awaiting Review
Component:  REST API           |    Version:  4.4
 Severity:  normal             |   Keywords:
  Focuses:  rest-api           |
-------------------------------+-----------------------------
 According to the `rest_do_request()` php docs, a `string` parameter type
 is accepted. Currently, this is forwarded to `rest_ensure_request()` which
 only accepts a `WP_REST_Request` object or an array. When a string
 argument is passed a request object is instantiated with a string value
 for the request attributes which is improper.

 Either the docs should be updated, or, ideally, the `rest_do_request` and
 `rest_ensure_request` functions would be updated.

 I could see those two functions either accepting a route path, like
 `/wp/v2/posts` or a full url, `www.example.org/wp-json/wp/v2`. Since those
 API functions are internal to the API instance, it makes most sense, in my
 mind at least, to accept a path style argument. Otherwise, there would be
 needless boiler plate: `rest_do_request( rest_url( '/wp/v2/posts' ) )`.

 The simplest implementation would then be to instantiate a
 `WP_REST_Request` object with the given path as the second constructor
 argument if the `$request` parameter is a string. However, this would make
 it impossible to quickly make a request with any query parameters
 attached.

 That gives us a few options.

 1. Change `rest_ensure_request` to do `WP_Rest_Request::from_url(
 rest_url( $request ) )` in case of a string argument.
 2. Introduce `WP_Rest_Request::from_path` that accepts a path and does the
 proper `parse_url` and `wp_parse_str` handling.
 3. Only accept a full URL to `rest_do_request`.

 If it might be confusing that `rest_ensure_request` accepts a path
 argument instead of a URL, conceivably it could accept both and switch on
 the presence of a `/` at the start of the string.

 Happy to submit a patch to whichever makes most sense.

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


More information about the wp-trac mailing list