How do I access the body of a WP API request in a custom route?

You can use $request->get_json_params() which will return an array of key => values.

With these conditions(possibly a few more):

  1. The client sending the request has Content-Type: application/json in the header
  2. There is a raw body like {"option":"siteColor","value":"ff0000"}.

https://developer.wordpress.org/reference/classes/wp_rest_request/get_json_params/

Leave a Comment