I have created a custom route in WP API (v2 beta 4) to set a site option. I'm using AngularJS to make the API call, and for some reason, I'm not able to access the data sent within the request. Here is what I have so far:

gvl.service('gvlOptionService', ['$http', function($http) {

    this.updateOption = function(option, value) {

        return $http({
          method  : 'POST',
          url     : wpAPIdata.gvlapi_base + 'options',
          data    : { "option" : option,
                      "value" : value
          headers : { 'Content-Type': 'application/x-www-form-urlencoded',
                      'X-WP-Nonce' : wpAPIdata.api_nonce



This successfully sends the request and the data posted looks something like this:


The request successfully makes it to my custom route and to the callback that I have specified. Here is that callback function within the class:

public function update_option( WP_REST_Request $request ) {

    if(isset($request['option']) && $request['option'] == 'siteColor') {
        $request_prepared = $this->prepare_item_for_database($request);
        $color_updated = update_option('site_color', $request_prepared['value'], false);

        if($color_updated) {
            $response = $this->prepare_item_for_response('site_color');
            $response->set_status( 201 );
            $response->header('Location', rest_url('/gvl/v1/options'));
            return $response;
        } else {
            // ...

    } else {
        return new WP_Error( 'cant_update_option', __( 'Cannot update option.' ), array( 'status' => 400 ) );


The problem is that this always fails and returns the WP_Error because $request['option'] is null.

When I var_dump($request), I see the JSON string in the ['body'] portion of the object, but it won't let me access that when calling that portion of the array. I've also tried using the methods for retrieving parameters noted in the documentation (http://v2.wp-api.org/extending/adding/), but none of those seem to return the data either. Am I missing something really basic here?

  • any luck with this?
    – jgraup
    Commented Nov 24, 2016 at 1:48

3 Answers 3


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"}.


  • This is exactly what I was looking for. Thanks! Commented Mar 8, 2019 at 14:17

In a previous answer is was able to access the data in a custom endpoint using

$parameters = $request->get_query_params(); 

Check the query params for option


Two options for getting the body of the request:

$body = $request->get_body();

$body_params = $request->get_body_params();


function my_awesome_func( WP_REST_Request $request ) {

    // Two ways to get the body from the request.
    $body = $request->get_body();
    $body_params = $request->get_body_params();

    // You can access parameters via direct array access on the object:
    $param = $request['some_param'];

    // Or via the helper method:
    $param = $request->get_param( 'some_param' );

    // You can get the combined, merged set of parameters:
    $parameters = $request->get_params();

    // The individual sets of parameters are also available, if needed:
    $parameters = $request->get_url_params();
    $parameters = $request->get_query_params();
    $parameters = $request->get_default_params();

    // Uploads aren't merged in, but can be accessed separately:
    $parameters = $request->get_file_params();

You can use $request->get_body()

Your Answer

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

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