[wp-trac] [WordPress Trac] #47834: Allow un-encoded output / other content types
WordPress Trac
noreply at wordpress.org
Sun Aug 4 19:56:18 UTC 2019
#47834: Allow un-encoded output / other content types
-------------------------+-----------------------------
Reporter: lkf_tanzt | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: REST API | Version: 5.2.2
Severity: normal | Keywords:
Focuses: |
-------------------------+-----------------------------
== The issue
I want to use the REST-API to provide other content types than JSON. It’s
possible to set the HTTP header `Content-Type` to other values but the
data in the the response are always json encoded so they are wrapped in ""
in the result echoed. The content type is ignored here.
I followed the program flow to this line:
https://core.trac.wordpress.org/browser/branches/5.2/src/wp-includes/rest-
api/class-wp-rest-server.php?rev=45281#L404
The `$result` is always json encoded, regardless of how the Content-type
header was set.
----
So what would be a possible solution?
== 1. Check for content-type header
One solution would be to query the header flied `Content-Type` from
`$result` (the headers are already sent in line 361) and encode the
content only if the header is set to `application/json` or a subtype of
this.
So the data are not encoded, if the content-type header is manually set
for a response to something else, and those other content-types would work
well. By default the content-type is set to `application/json` and the
data is encoded, so everything would work as before.
== 2. Introduce filterable encode switch
We could introduce a flag {{{$do_json_encode}}} which would allow to skip
the encoding part by adding a filter on this and returning false.
A similar thing is done in
[https://core.trac.wordpress.org/browser/branches/5.2/src/wp-includes
/rest-api/class-wp-rest-server.php?rev=45281#L380 Line 380] with
`$served`. So this could look like the following:
{{{#!php
$do_json_encode = apply_filters( 'rest_do_json_encode', true, $result,
$request, $this );
if ( $do_json_encode ){
// the currently existing line
$result = wp_json_encode( $result );
}
}}}
Since the value defaults to `true` the default behaviour would not change
but we would introduce the option to alter this with a filter and since we
provide `$result` and `$request` the user/developer can decide on his own
if he want’s to encode the data or not.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/47834>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list