8
votes

I am currently working on a project which requires a WordPress website and a simple REST api. I discovered that WordPress has its own REST api and decided to extend its functionality to meet my needs. All I need to do is to have endpoints for GET and POST requests that retrieve/insert data from/to a table which is not directly related to WordPress (but in the same database). I successfully implemented all GET requests, however, I am struggling to get the POST ones working.
I have this route register defined:

register_rest_route('api/v1', 'create-player/', array(
        'methods' => 'POST',
        'callback' => 'create_player',
));

The client sends a request through ajax call which is expected to hit the endpoint from the route above. This is the ajax:

    $.ajax({
       method: "POST",
       url: '/wp-json/api/v1/create-player/',
       data : JSON.stringify(data),
       contentType: 'applcation/json',
       beforeSend: function (xhr){
           xhr.setRequestHeader("X-WP-None", locData.nonce);
           console.log('beforeSend');
       },
       success: function(response){
           console.log("success" + response);
       },
       fail: function (response){
           console.log("fail" + response);
       }
    });

I am not sure how to build the POST route register from the REST api, the other GET requests have an attribute args that map directly to the parameters passed in the endpoint. Do I need something like that to handle the request data when using POST? How do I get the data type passed from the ajax and then use it in my function create_player(); The WP REST API documentation seems to be incomplete and all of the information I found uses endpoints for built-in WordPress features such as posts/authors/blogs etc. but I don't need that, I just want to use the provided functionality and create my own interface. Thank you.

3
FYI, there's nothing REST about that API.Evert
@Evert Okay, I understand that the POST and GET methods are not under the same endpoint (in this case for /player) and having a "more restful" api is exactly what I am trying to achieve. In the example above I separated the POST method from the other endpoints to make my question clearer. Although I was not able to understand how the WP REST API POST requests work, I managed to implement a silly workaround by passing a JSON to the url when sending a POST request, this way I can get the actual data and manipulate it. But I would still love to understand how to handle this situation properly...bassment
I can't help you much with wordpress, but if you are interested in developing better REST apis, I'd recommend you read this: martinfowler.com/articles/richardsonMaturityModel.html if you are not 'Level 3' you are technically not REST.Evert
Your request has a misspelling in the contentType param for $.ajax call.Nathan Moinvaziri

3 Answers

13
votes

in your callback function you can use something like this:

 $param = $request->get_param( 'some_param' );

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

https://www.coditty.com/code/wordpress-api-custom-route-access-post-parameters

12
votes

Finally found it! In order to access the body of the POST request use $request->get_body(); in your register_rest_route callback method.

1
votes

Add POST in methods while registering route and in your callback function access your POST variables via $request array. That's it.