0

I'm writing a plugin which includes a custom endpoint for the WordPress REST API, the goal of this is to supply an importer function for an existing content type. For some reason, I'm not getting the body of the request passed into the callback function. The goal is to pass in xml Data into an existing function.

add_action( 'rest_api_init',  function () {
  $routes= new new_Custom_Route;
   return $routes->register_routes();
 });
class new_Custom_Route extends WP_REST_Controller {

  public function register_routes() {
   $version = '2';
$namespace = 'new_rest/v' . $version;
$base = 'route';
register_rest_route( $namespace, '/' . $base, array(
  array(
    'methods'         => WP_REST_Server::READABLE,
    'callback'        => array( $this, 'new_rest_get_items' ),
  ),
  array(
    'methods'         => WP_REST_Server::CREATABLE,
    'callback'        => array( $this, 'new_rest_create_item' ),
  ),
) );

public function new_rest_create_item($request) {
    $parameters = $request->get_params();
    $test_array=array();
    $test_array[]=$request;
    $test_array[]=$parameters;
    $body = $request->get_body_params();
    $test_array[]=$body;
    $insert_result=insertdatafunction($body);
if (!is_numeric($insert_result)) {
    return new WP_REST_Response($test_array, 418 );
} else {
       return new WP_REST_Response($test_array, 200 );
      //return new WP_REST_Response($insert_result, 200 );
}
}

When I use postman to do a POST request to new_rest/v2/route?test=23 This code gives an output of 418 response ( expected, yes it's a valid response code, no it's not a coffee pot ). the body of the response is as follows:

[
    {},
   { "test": "23"},
    []
]

So I can get the parameters from the url, but not the body of the the request ( which is what I need to pass into the insertdatafunction()

I've also tried passing the request in in the following ways

public function new_rest_create_item(WP_REST_Request $request) {

and

public function new_rest_create_item(\WP_REST_Request $request) {

with exactly the same results....

Additional information: The body of the request is a valid xml document being produced by an external system, and is being included in the call. I'm creating a custom endpoint due to the nature of of the custom post type, the amount of meta fields, and to hook into the existing importer functions that were written for this content type.

  • Seeing at your code, the only sure thing is that response you get is becasue insertdatafunction() returns a non-numeric value. You should debug $request in some other way. – cybmeta Sep 15 '17 at 11:44
  • @cybmeta : yeah, because the body being passed into it is empty, which is what the issue is here, I'm asking why I'm not getting the body of the request when I call it. - on that note, do owe you a thanks though, I think i'd accidentally deleted a line of code. – Jesterscup Sep 15 '17 at 11:47
  • I'm not very familiar with this class, is it possible you need to call $request->parse_body_params() first before ->get_body_params()? – kero Sep 15 '17 at 11:57
  • thanks @kero ... well kinda, it turns out that it should've just been $request->get_body(); ... – Jesterscup Sep 15 '17 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.