Vue 3 - HTTP DELETE Request Examples
Tutorial built with Vue 3.2.45 using the Composition API
Below is a quick set of examples to show how to send HTTP DELETE requests from Vue 3 to a backend API using fetch()
which comes built into all modern browsers.
Other Vue 3 HTTP examples: GET, POST, PUT, PATCH.
Tutorial contents
- Simple DELETE request
- DELETE request with async/await
- DELETE request with headers set
- DELETE request with error handling
Simple DELETE request
This sends an HTTP DELETE request to the Test JSON API which is a fake online REST API that includes a product details route (/products/{id}
) route that responds to DELETE
requests with { "message": "Product deleted" }
.
The JSON response from the API is assigned to the data
ref variable and the message is rendered in the component template.
<script setup>
import { ref } from 'vue';
const data = ref(null);
// Simple DELETE request with fetch
fetch('https://testapi.jasonwatmore.com/products/1', { method: 'DELETE' })
.then(response => response.json())
.then(x => data.value = x);
</script>
<template>
<div class="card text-center m-3">
<h5 class="card-header">Simple DELETE Request</h5>
<div class="card-body">Message: {{data?.message}}</div>
</div>
</template>
Vue 3 fetch component at https://stackblitz.com/edit/vue-3-http-delete-request-examples?file=src%2FDeleteRequest.vue
DELETE request using fetch with async/await
This sends the same DELETE request from Vue 3 using fetch, but this version uses a couple of top-level await
expressions in the <script setup>
block to wait for the promises to return (instead of using the promise then()
method as above). The top-level await
results in the setup function being compiled as async setup()
.
NOTE: async components must be wrapped with the Vue 3 <Suspense>
component in order to render. Suspense is still an experimental feature at the time I'm writing this so the way it works may change in future. For more info see https://vuejs.org/guide/built-ins/suspense.html.
<script setup>
// DELETE request using fetch with async/await
const response = await fetch('https://testapi.jasonwatmore.com/products/1', { method: 'DELETE' });
const data = await response.json();
</script>
<template>
<div class="card text-center m-3">
<h5 class="card-header">DELETE Request with Async/Await</h5>
<div class="card-body">Message: {{data?.message}}</div>
</div>
</template>
Vue 3 fetch component at https://stackblitz.com/edit/vue-3-http-delete-request-examples?file=src%2FDeleteRequestAsyncAwait.vue
DELETE request using fetch with set HTTP headers
This sends the same DELETE request again from Vue 3 using fetch with a couple of headers set, the HTTP Authorization
header and a custom header My-Custom-Header
.
<script setup>
import { ref } from 'vue';
const data = ref(null);
// DELETE request using fetch with set headers
const requestOptions = {
method: 'DELETE',
headers: {
'Authorization': 'Bearer my-token',
'My-Custom-Header': 'foobar'
}
};
fetch('https://testapi.jasonwatmore.com/products/1', requestOptions)
.then(response => response.json())
.then(x => data.value = x);
</script>
<template>
<div class="card text-center m-3">
<h5 class="card-header">DELETE Request with Set Headers</h5>
<div class="card-body">Message: {{data?.message}}</div>
</div>
</template>
Vue 3 fetch component at https://stackblitz.com/edit/vue-3-http-delete-request-examples?file=src%2FDeleteRequestSetHeaders.vue
DELETE request using fetch with error handling
This sends a DELETE request from Vue 3 to an invalid url on the API then assigns the error to the errorMessage
ref variable and logs the error to the console.
Error handling with the Fetch API
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. For more info see Fetch - Error Handling for Failed HTTP Responses and Network Errors.
<script setup>
import { ref } from 'vue';
const data = ref(null);
const errorMessage = ref(null);
// DELETE request using fetch with error handling
fetch('https://testapi.jasonwatmore.com/invalid-url', { method: 'DELETE' })
.then(async response => {
const isJson = response.headers.get('content-type')?.includes('application/json');
data.value = 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);
}
})
.catch(error => {
errorMessage.value = error;
console.error("There was an error!", error);
});
</script>
<template>
<div class="card text-center m-3">
<h5 class="card-header">DELETE Request with Error Handling</h5>
<div class="card-body">Error message: {{errorMessage}}</div>
</div>
</template>
Vue 3 fetch component at https://stackblitz.com/edit/vue-3-http-delete-request-examples?file=src%2FDeleteRequestErrorHandling.vue
Need Some Vue 3 Help?
Search fiverr for freelance Vue 3 developers.
Follow me for updates
When I'm not coding...
Me and Tina are on a motorcycle adventure around Australia.
Come along for the ride!