Published:

Vue + Fetch - HTTP DELETE Request Examples

Below is a quick set of examples to show how to send HTTP DELETE requests from Vue to a backend API using fetch() which comes bundled with all modern browsers.

Other HTTP examples available:


Simple DELETE request with fetch

This sends an HTTP DELETE request to the Reqres api which is a fake online REST api that includes a /api/posts/:id route that responds to DELETE requests with a HTTP 204 response. When the response is received the Vue component displays the status message 'Delete successful'.

created() {
  // Simple DELETE request with fetch
  fetch('https://reqres.in/api/posts/1', { method: 'DELETE' })
    .then(() => this.status = 'Delete successful');
}

Example Vue component at https://codesandbox.io/s/vue-fetch-http-delete-request-examples-bcrmt1?file=/app/DeleteRequest.vue


DELETE request using fetch with async/await

This sends the same DELETE request from Vue using fetch, but this version uses an async function and the await javascript expression to wait for the promise to return (instead of using the promise then() method as above).

async created() {
  // DELETE request using fetch with async/await
  await fetch('https://reqres.in/api/posts/1', { method: 'DELETE' });
  this.status = 'Delete successful';
}

Example Vue component at https://codesandbox.io/s/vue-fetch-http-delete-request-examples-bcrmt1?file=/app/DeleteRequestAsyncAwait.vue


DELETE request using fetch with error handling

This sends a DELETE request from Vue using fetch to an invalid url on the api then assigns the error message to the errorMessage component data property and logs the error to the console.

The fetch() function will automatically throw an error for network errors but not for HTTP errors such as 4xx or 5xx responses. For HTTP errors we can check the response.ok property to see if the request failed and reject the promise ourselves by calling return Promise.reject(error);. This approach means that both types of failed requests - network errors and http errors - can be handled by a single catch() block.

created() {
  // DELETE request using fetch with error handling
  fetch('https://reqres.in/invalid-url', { method: 'DELETE' })
    .then(async response => {
      const isJson = response.headers.get('content-type').includes('application/json');
      const data = isJson && await response.json();

      // check for error response
      if (!response.ok) {
          // get error message from body or default to response status
          const error = (data && data.message) || response.status;
          return Promise.reject(error);
      }

      this.status = 'Delete successful';
    })
    .catch(error => {
      this.errorMessage = error;
      console.error('There was an error!', error);
    });
}

Example Vue component at https://codesandbox.io/s/vue-fetch-http-delete-request-examples-bcrmt1?file=/app/DeleteRequestErrorHandling.vue


DELETE request using fetch with set HTTP headers

This sends the same DELETE request again from Vue using fetch with a couple of headers set, the HTTP Authorization header and a custom header My-Custom-Header.

created() {
  // DELETE request using fetch with set headers
  const requestOptions = {
    method: 'DELETE',
    headers: { 
      'Authorization': 'Bearer my-token',
      'My-Custom-Header': 'foobar'
    }
  };
  fetch('https://reqres.in/api/posts/1', requestOptions)
    .then(() => this.status = 'Delete successful');
}

Example Vue component at https://codesandbox.io/s/vue-fetch-http-delete-request-examples-bcrmt1?file=/app/DeleteRequestSetHeaders.vue

 


Subscribe or Follow Me For Updates

Subscribe to my YouTube channel or follow me on Twitter, Facebook or GitHub to be notified when I post new content.

Other than coding...

I'm currently attempting to travel around Australia by motorcycle with my wife Tina on a pair of Royal Enfield Himalayans. You can follow our adventures on YouTube, Instagram and Facebook.


Need Some Vue Help?

Search fiverr to find help quickly from experienced Vue developers.



Supported by