• Hello! I have been trying to create a custom REST API and have had no issues setting up a page to perform the $_GET function. I am now working on consuming the $_POST function, however when I try pass any values the page 404’s. I suspect it is something to do with permalinks however have not been able to resolve it. I have been using the POST method described here (exactly as is): https://dzikrirobbi.medium.com/rest-api-with-php-get-post-put-delete-8365fe092618
    I have been using Postman to test and the exact same code works flawlessly on the example url. I am aware WP has inbuilt REST functionality, which I would rather not use however may explore if I am unable to resolve this.

    Any help is appreciated!

Viewing 3 replies - 1 through 3 (of 3 total)
  • You can test the response and your code by applying changes.
    For example: Send a GET Request on the same endpoint(make sure you allow GET on that endpoint) and see what you get 404 or a valid response.

    It will confirm that the endpoint is current then you check & test other things and track down the problem.

    Thread Starter claytybabe

    (@claytybabe)

    Hi Paul,

    I ended up using the WP inbuilt REST API, I only set it up to use the POST method and built around that (terrible practice I know but it works – I did find the response time is disgustingly slow compared to a custom built stand alone PHP API). I used this tutorial to get me on the right track: https://awhitepixel.com/blog/in-depth-guide-in-creating-and-fetching-custom-wp-rest-api-endpoints/

    Anyway, I’ll assume you have a custom child theme you’re working from.

    In your child theme:
    1. Create functions.php if you haven’t already.
    2. Add the following action and function to functions.php:

    add_action('rest_api_init', function() {
      register_rest_route('yourownpath/v1', 'post', array( //register the route to call the WP API from
        'methods' => WP_REST_SERVER::CREATABLE, //Set the method (i.e. get, post etc.) WP uses a different naming convention though
        'callback' => 'get_awesome_params',
        'args' => array(), //can be set to accept specific things other than an undefined array, ref awhitepixels api example, the empty array will accept any key passed to it so if you dont set specific args you will need to perform some sort of validation in the function below.
        'permission_callback' => function () {
          return true;
        }
      ));
    });
    
    function get_awesome_params(WP_REST_Request $request) {
    //this is where you can add your own code - i.e. what happens with the keys passed in the POST request, the below just returns a response saying what the keys were to demonstrate the return.
          return new WP_REST_Response($parameters, 200);
    }

    3. Test it:
    To consume the API you use the following address structure (in Postman for example):
    https://yourwebsite.com/wp-json/yourownpath/v1/post
    If you’re passing keys in the string (really turning it into a GET) it would be something like this:
    https://yourwebsite.comt/wp-json/yourownpath/v1/post/key1=thing&key2=otherthing

    Goodluck!

    Thread Starter claytybabe

    (@claytybabe)

    Sorry I missed a line in the get_awesome_params function, it should look like this!

    function get_awesome_params(WP_REST_Request $request) {
    //this is where you can add your own code - i.e. what happens with the keys passed in the POST request, the below just returns a response saying what the keys were to demonstrate the return.
          $parameters = $request->get_params();
          return new WP_REST_Response($parameters, 200);
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom REST API 404’ is closed to new replies.