0

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 ?

2
  • What does get_body() return? Have you set Content-Type to application/json? In Postman you need to select raw for the body and select JSON from the dropdown. Aug 25, 2022 at 8:15
  • @JacobPeattie get_body() returns 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 ?
    – Valenten
    Aug 25, 2022 at 8:26

1 Answer 1

1

The value you're trying to send in the body is not valid JSON:

{
    "userID" = 3
}

JSON looks like this:

{
    "userID": 3
}
1
  • indeed! thanks !
    – Valenten
    Aug 25, 2022 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.