[wp-trac] [WordPress Trac] #43691: class-wp-rest-server sends response body regardless of actual response code or result type

WordPress Trac noreply at wordpress.org
Wed Apr 4 11:59:07 UTC 2018


#43691: class-wp-rest-server sends response body regardless of actual response code
or result type
----------------------------+-----------------------------
 Reporter:  matthias.thiel  |      Owner:
     Type:  defect (bug)    |     Status:  new
 Priority:  normal          |  Milestone:  Awaiting Review
Component:  REST API        |    Version:  trunk
 Severity:  normal          |   Keywords:
  Focuses:  rest-api        |
----------------------------+-----------------------------
 The WP Rest Server always echoes the result of `wp_json_encode` unless
 there is an error.

 See line `410` of class-wp-rest-server.php

 {{{
 echo $result;
 }}}

 A use case where this leads to an undesired behaviour is when using the
 HTTP Status Code 204 which implies no response body being sent. Returning
 a rest response with a status code 204 will send a body at the moment.

 There are two problems with this behaviour:

 1. A response status 204 should not send any body content
 2. A response data of `null` (`new \WP_REST_Response( null, 204 )`)  will
 be sent as the (string) `null`

 Most clients are pretty forgiving and will ignore the request body but
 MacOS Safari will not complete the request in this situation and will try
 to parse the `null` response as JSON. I found the behaviour is also server
 configuration specific as some configurations will strip the response body
 automatically and some will not.

 Since there is no consistency in treating a 204 response by servers and
 browsers, I would suggest to stick to the spec
 (https://tools.ietf.org/search/rfc2616#section-4.3) and not return a
 response at all.

 I can see two ways to achieve this:

 1. Consult the response status to decide whether or not to return a
 response body, which is spec compliant
 2. Consult the response data and treat `null` by not sending a response
 body, which honours the semantic of `null`.

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


More information about the wp-trac mailing list