I am registering a custom route like this :
add_action('rest_api_init', function () {
register_rest_route( 'professeurEQ', 'statistics', array(
'methods' => 'POST',
'callback' => 'test_proeq_post',
));
});
The callback is written like this :
function test_proeq_post($request) {
$jsonParams = $request->get_body_params();
return var_dump($jsonParams);
}
I am sending the POST request using Postman and the following body :
{
"userID" = 3
}
Whatever I am doing, the $jsonParams is always empty. The same thing happens with methods like get_json_params or get_params or get_param.
get_body() works, but I would like to receive an array and use the get_body_params() method.
If I do
var_dump($request)
Then I see the full request, and I noticed all the parameters are protected. I think it must be the cause of the problem, but how to solve it ?
get_body()
return? Have you setContent-Type
toapplication/json
? In Postman you need to select raw for the body and select JSON from the dropdown.array (size=0) empty
In Postman I have selected RAW for the body and JSON in the dropdown (there was a mistake here initially but after correcting it, I get the same result. But thanks for this already). How to set Content-type ?