Published: September 21 2021

Fetch - HTTP DELETE Request Examples

Below is a quick set of examples to show how to send HTTP DELETE requests to an 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 status message 'Delete successful' is displayed in the #delete-request .status element.

// Simple DELETE request with fetch
const element = document.querySelector('#delete-request .status');
fetch('https://reqres.in/api/posts/1', { method: 'DELETE' })
    .then(() => element.innerHTML = 'Delete successful');

Example Fetch DELETE request at https://stackblitz.com/edit/fetch-http-delete-request-examples?file=delete-request.js


DELETE request using fetch with async/await

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

(async () => {
    // DELETE request using fetch with async/await
    const element = document.querySelector('#delete-request-async-await .status');
    await fetch('https://reqres.in/api/posts/1', { method: 'DELETE' });
    element.innerHTML = 'Delete successful';
})();

Example Fetch DELETE request at https://stackblitz.com/edit/fetch-http-delete-request-examples?file=delete-request-async-await.js


DELETE request using fetch with error handling

This sends a DELETE request with fetch to an invalid url on the api then writes the error message to the parent of the #delete-request-error-handling .status element and logs the error to the console.

// DELETE request using fetch with error handling
const element = document.querySelector('#delete-request-error-handling .status');
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);
        }

        element.innerHTML = 'Delete successful';
    })
    .catch(error => {
        element.parentElement.innerHTML = `Error: ${error}`;
        console.error('There was an error!', error);
    });

Example Fetch DELETE request at https://stackblitz.com/edit/fetch-http-delete-request-examples?file=delete-request-error-handling.js


DELETE request using fetch with set HTTP headers

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

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

Example Fetch DELETE request at https://stackblitz.com/edit/fetch-http-delete-request-examples?file=delete-request-set-headers.js

 


Need Some Fetch Help?

Search fiverr for freelance Fetch developers.


Follow me for updates

On Twitter or RSS.


When I'm not coding...

Me and Tina are on a motorcycle adventure around Australia.
Come along for the ride!


Comments


Supported by