$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - REST Endpoint API 404|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugin development - REST Endpoint API 404

matteradmin9PV0评论

I recently have been led to REST Endpoints within WordPress on another one of my posts.

However following the guides/provided solution results in a 404 of the expected endpoint API url.

My code is as follows:

wp-content/plugins/vendor-module/admin/admin-endpoint.php

<?php
    class Import_Csv
    {
        public function register_routes()
        {
            $version = 1;
            $namespace = sprintf('vendor/v%u', $version);
            $base = '/import';

            \register_rest_route(
                $namespace,
                $base,
                [
                    [
                        'methods' => \WP_REST_Server::CREATABLE,
                        'callback' => [$this, 'import_csv'],
                        'permission_callback' => [$this, 'get_import_permissions_check'],
                        'args' => []
                    ]
                ]
            );
        }

        public function get_import_permissions_check($req)
        {
            return true;
        }

        public function import_csv_file($req)
        {
            # the import process
            return new \WP_REST_Response($data, 200);
        }
    }

then in my plugins file I add the action:

wp-content/plugins/vendor-module/vendor-module.php

require_once 'admin/admin-endpoint.php';
add_action('rest_api_init', function()
{
    $import_csv = new \Import_Csv;
    $import_csv->register_routes();
});

I then go to this /index.php/wp-json/vendor/v1/import/ to test if it works but it returns a 404 error. What am I doing wrong in trying to create my endpoint?

**Edit: ** further testing

Going to the url /index.php/wp-json/vendor/v1/ shows this info:

{
  "namespace": "vendor/v1",
  "routes": {
    "/vendor/v1": {
      "namespace": "vendor/v1",
      "methods": [
        "GET"
      ],
      "endpoints": [
        {
          "methods": [
            "GET"
          ],
          "args": {
            "namespace": {
              "required": false,
              "default": "vendor/v1"
            },
            "context": {
              "required": false,
              "default": "view"
            }
          }
        }
      ],
      "_links": {
        "self": "/index.php/wp-json/vendor/v1"
      }
    },
    "/vendor/v1/import": {
      "namespace": "vendor/v1",
      "methods": [
        "POST"
      ],
      "endpoints": [
        {
          "methods": [
            "POST"
          ],
          "args": []
        }
      ],
      "_links": {
        "self": "/index.php/wp-json/vendor/v1/import"
      }
    }
  },
  "_links": {
    "up": [
      {
        "href": "/index.php/wp-json/"
      }
    ]
  }
}

So I can clearly see my link in that JSON string but for whatever reason, it's 404'ing when I actually go to it..

I recently have been led to REST Endpoints within WordPress on another one of my posts.

However following the guides/provided solution results in a 404 of the expected endpoint API url.

My code is as follows:

wp-content/plugins/vendor-module/admin/admin-endpoint.php

<?php
    class Import_Csv
    {
        public function register_routes()
        {
            $version = 1;
            $namespace = sprintf('vendor/v%u', $version);
            $base = '/import';

            \register_rest_route(
                $namespace,
                $base,
                [
                    [
                        'methods' => \WP_REST_Server::CREATABLE,
                        'callback' => [$this, 'import_csv'],
                        'permission_callback' => [$this, 'get_import_permissions_check'],
                        'args' => []
                    ]
                ]
            );
        }

        public function get_import_permissions_check($req)
        {
            return true;
        }

        public function import_csv_file($req)
        {
            # the import process
            return new \WP_REST_Response($data, 200);
        }
    }

then in my plugins file I add the action:

wp-content/plugins/vendor-module/vendor-module.php

require_once 'admin/admin-endpoint.php';
add_action('rest_api_init', function()
{
    $import_csv = new \Import_Csv;
    $import_csv->register_routes();
});

I then go to this http://site.local/index.php/wp-json/vendor/v1/import/ to test if it works but it returns a 404 error. What am I doing wrong in trying to create my endpoint?

**Edit: ** further testing

Going to the url http://site.local/index.php/wp-json/vendor/v1/ shows this info:

{
  "namespace": "vendor/v1",
  "routes": {
    "/vendor/v1": {
      "namespace": "vendor/v1",
      "methods": [
        "GET"
      ],
      "endpoints": [
        {
          "methods": [
            "GET"
          ],
          "args": {
            "namespace": {
              "required": false,
              "default": "vendor/v1"
            },
            "context": {
              "required": false,
              "default": "view"
            }
          }
        }
      ],
      "_links": {
        "self": "http://site.local/index.php/wp-json/vendor/v1"
      }
    },
    "/vendor/v1/import": {
      "namespace": "vendor/v1",
      "methods": [
        "POST"
      ],
      "endpoints": [
        {
          "methods": [
            "POST"
          ],
          "args": []
        }
      ],
      "_links": {
        "self": "http://site.local/index.php/wp-json/vendor/v1/import"
      }
    }
  },
  "_links": {
    "up": [
      {
        "href": "http://site.local/index.php/wp-json/"
      }
    ]
  }
}

So I can clearly see my link in that JSON string but for whatever reason, it's 404'ing when I actually go to it..

Share Improve this question edited Dec 12, 2018 at 10:24 treyBake asked Dec 12, 2018 at 10:06 treyBaketreyBake 12712 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

There are few problems:

  1. you are using \WP_REST_Server::CREATABLE which is executed only when the request type is POST, if you are just opening the URL in the browser use \WP_REST_Server::READABLE instead (or \WP_REST_Server::ALLMETHODS to accept any request type).

  2. your callback is 'callback' => [$this, 'import_csv'], but it should be 'callback' => [$this, 'import_csv_file'], as the import_csv() method is not defined so i am guessing you meant import_csv_file().

  3. the return new \WP_REST_Response($data, 200); should be return new \WP_REST_Response($req, 200); as $data variable does not seem to be defined.

The complete code should look like this

class Import_Csv
{
    public function register_routes()
    {
        $version = 1;
        $namespace = sprintf('vendor/v%d', $version);
        $base = '/import/';

        \register_rest_route(
            $namespace,
            $base,
            [
                [
                    'methods' => \WP_REST_Server::READABLE,
                    'callback' => [$this, 'import_csv_file'],
                    'permission_callback' => [$this, 'get_import_permissions_check'],
                    'args' => []
                ]
            ]
        );
    }

    public function get_import_permissions_check($req)
    {
        return true;
    }

    public function import_csv_file($req)
    {
        # the import process
        return new \WP_REST_Response($req, 200);
    }
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far