1

I have wrote an endpoint in WordPress and it works just fine. The problem is with the URL. The current format of the URL request must be like this:

https://iotkidsiq.com/wp-json/zaindob/v1/sync_order/key=011900/msisdn=019009

However, The URL request must be in this format:

https://iotkidsiq.com/wp-json/zaindob/v1/sync_order?key=011900&msisdn=019009

How do I format that?

Here is my code:

register_rest_route( 'zaindob/v1', '/sync_order/' . 'key=' . '(?P<key>\d+)' .  '/msisdn=' . '(?P<msisdn>\d+)' , array( 

    'methods' => 'GET',
    'callback' => 'updatetable',
  ) );
} );

1 Answer 1

1

You don't. Query args are not part of the route URL. Your endpoint URL is:

https://iotkidsiq.com/wp-json/zaindob/v1/sync_order

So needs to be registered as:

register_rest_route( 'zaindob/v1', '/sync_order', array( 
    'methods' => 'GET',
    'callback' => 'updatetable',
) );

key and msisdn are arguments that are sent to your endpoint. To define these use set the args property of the endpoint options:

register_rest_route( 'zaindob/v1', '/sync_order', array( 
    'methods'  => 'GET',
    'callback' => 'updatetable',
    'args'     => array(
        'key'    => array(
            'type'     => 'integer',
            'required' => true,
        ),
        'msisdn' => array(
            'type'     => 'integer',
            'required' => true,
        ),
    ),
) );

Now your endpoint callback can accept the key and msisdn paramaters, and they are required for the endpoint to return a result:

function updatetable( $request ) {
    $key    = $request->get_param( 'key' );
    $msisdn = $request->get_param( 'msisdn' );

    // etc.
}
2
  • thank you so much for your help. It works perfectly now. Really appreciate it Commented Sep 6, 2019 at 10:09
  • Thank you so much. Commented May 17, 2020 at 17:31

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.