WP_REST_Response - How to return Gzip answer and add Content-encoding header?
This code runs correctly :
function get_travelbooks($request)
{
global $tsp_mdlObj;
$result = $tsp_mdlObj-read_travelbooks();
return new WP_REST_Response($result, 200);
}
Now I would like send a gzip answer. So I try :
function get_travelbooks($request)
{
global $tsp_mdlObj;
$result = $tsp_mdlObj-read_travelbooks();
$result = gzencode(json_encode($result), 9);
$response = new WP_REST_Response($result, 200);
$response-header('Content-Encoding', 'gzip');
return $response;
}
But this code doesn't work.
The content of $result before gzencode(json_encode is an array :
Array
(
[0] = stdClass Object
(
[id] = 5
[name] = travel 2021
)
[1] = stdClass Object
(
[id] = 4
[name] = travel 2020
)
[2] = stdClass Object
(
[id] = 3
[name] = travel 2019
)
)
hereafter is the register_rest_route used
add_action('rest_api_init', function () {
register_rest_route( 'travelsteps/v1', 'travelbooks',array(
'methods' = 'GET',
'callback' = 'get_travelbooks',
'permission_callback' = '__return_true',
));
});
I got the following error in console :
xhr.js:177 GET xxxxxx/v1/travelbooks net::ERR_CONTENT_DECODING_FAILED 200 (OK)
And the network header Content-Encoding: gzip is present.
And inside the network response tab i get the following message :
no data found for resource with given identifier
Any suggestion to fix this problem or a resource regarding gzip for a wp custom end point api rest. Thanks.