Tuesday 29 December 2020

Passing form data to API custom route with WordPress

Getting the following 500 code response when making my API call: PUT https://my-site.com/wp/wp-json/contact/v1/send 500

In functions.php my WP custom route is defined thus:

function sendContactMail(WP_REST_Request $request) {
}

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

Here's how I'm making my API call:

formData.append('contact_name', this.contactName)
formData.append('contact_email', this.contactEmail)
formData.append('contact_message', this.contactMessage)

this.$axios.$put('https://my-site.com/wp/wp-json/contact/v1/send', formData)
  .then((res) => {
    this.ApiResponse = res
  })
  .catch((err) => {
    this.$toast.error(err.response)
  })

Why am I getting a 500 error?



from Passing form data to API custom route with WordPress

No comments:

Post a Comment