Search code examples
javascriptwordpressrestrouteswordpress-rest-api

WordPress API route unreachable


When calling Axios put() method with my WordPress custom route I'm getting this error in console:

PUT http://my-project.com/wp/wp-json/contact/v1/send 404 (Not Found)

Here's how I define my custom route in WP's functions.php:

add_action('rest_api_init', function () {
  register_rest_route( 'contact/v1', 'send', array(
    'methods' => 'POST',
    'callback' => 'sendContactMail'
  ));
});

Here's the call in my app (Vue.js):

this.$axios.$put(`${this.baseUrl}/wp-json/contact/v1/send`, formData)
  .then((res) => {
    this.success = true
  })
  .catch((err) => {
    this.$toast.error(err.response)
  })

What am I doing wrong?


Solution

  • In the methods section of the register_rest_route call, only the POST method is allowed, but your code is using PUT.

    You can either change your axios call to axios.post or add the PUT method in your route definition. To do that, change methods, which can be a string of comma-separated HTTP methods or an array of strings like:

    'methods' => 'POST,PUT',
    

    OR

    'methods' => ['POST','PUT'],