WordPress wp rest api v2 error 400 (Bad Request)” Object {code: “rest_invalid_param”, message: “Invalid parameter(s): id”, data: Object}

On local host request to my WordPress app that is using the “wp rest api v2” is working as expected with no issues. Here is an example of my local host post request:

POST “http://127.0.0.1/plugin namespace/plugin-name/wp-json/plugin namespace/rest of the api url”

Read More

But on my hosted site the request is returning an error, similar request to my hosted site:

POST “http://my-domain.com/plugin-name/wp-json/plugin namespace/rest of the api url”

And I get on chrome console “400 (Bad Request)”
Object {code: “rest_invalid_param”, message: “Invalid parameter(s): id”, data: Object}

Another note the payload is identical in both requests:

$http.post(url, { id : 1, shortMessage:”a b c”}, {headers: {‘X-WP-Nonce’: “my nonce value”}}); // I am using angular $http to make the post request

I wrote how the url’s look like above.

In my plugin php code I wrote:

function myplugin_register_endpoints(){

register_rest_route(

    'plugin namespace',
    '/rest of the api url',
    array(
        'methods' => 'POST',
        'callback' => 'my_end_point',
        'args' => array(
                'id' => array(
                    'required' => true,
                    'validate_callback' => function($param, $request, $key) {
                        return is_numeric( $param ) and ! is_null(get_post($param));//numeric post id value and there is valid post for this id
                    },
                    'sanitize_calback' => 'absint'
                )
        ),
        'permission_callback' => function (WP_REST_Request $request) {

            if(!is_user_logged_in()){
                return new WP_Error('login error',__('You are not logged in','blabla'));
            }
            return true;

        }
    )

);
}

add_action('rest_api_init','myplugin_register_endpoints');

function my_end_point(WP_REST_Request $request) {

    global $current_user;
    $current_user = wp_get_current_user();

    if($my_var){
        return array('message' => $message,'items' => $items,'item'=>$item);
    } else {
        return new WP_Error('add friend error',__('message'),$request['id']);
    }
}

I need to understand why the request is failing on my hosted site.

Thanks
K

Related posts

Leave a Reply

1 comment

  1. I found my mistake.

    it was on this line:

    return is_numeric( $param ) and ! is_null(get_post($param));//numeric post id value and there is valid post for this id

    I changed it to:

    return is_numeric( $param );

    It happened because I copy paste the register_rest_route(…){…} part without modifying the ‘validate_callback’ logic.

    the param in this ‘validate_callback’ is user ID not post ID. so the part checking if the param is a post ID( … and ! is_null(get_post($param)); … ) is not relevant for this end point.
    Therefore after omitting this check the end point passed the ‘validate_callback’ and stopped returning an error.