Site icon Hip-Hop Website Design and Development

Inspecting WP_Rest_Request

I’m implementing a callback for a GET Wp Rest request, which receives a WP_Rest_Request object.

function create_on_demand_post( WP_REST_Request $request ) {
    $params = $request->get_json_params();
    // etc...
}

There are class methods which retrieve the request data, but I’m wondering if there’s a way to access the data in this ArrayAccess implementation as I would with an array or object:

WP_REST_Request
    method:"POST"
    params:array(6)
    headers:array(8)
    body:"{"title": "From Python", "status": "publish", "content": "This is a post created using rest API", etc...}"
    route:"/my-plugin/v1/posts"
    attributes:array(7)
    parsed_json:true
    parsed_body:false

I have tried $request->body, which doesn’t evaluate and $request['body'], as well as $request['body'][0], both of which return null.

Are you only able to access the attributes using the provided methods?