Sometimes, we want to use JavaScript fetch to make DELETE and PUT requests.
In this article, we’ll look at how to use JavaScript fetch to make DELETE and PUT requests.
How to use JavaScript fetch to make DELETE and PUT requests?
To use JavaScript fetch to make DELETE and PUT requests, we can call fetch
with the method
option.
For instance, we write
const createNewProfile = async (profile) => {
const formData = new FormData();
formData.append("first_name", profile.firstName);
formData.append("last_name", profile.lastName);
formData.append("email", profile.email);
const response = await fetch("http://example.com/api/v1/registration", {
method: "POST",
body: formData,
});
return response.json();
};
const json = await createNewProfile(profile);
to call fetch
with an object that has the method
property set to a string with the request method.
We set body
to the request body.
And then we return the response JSON promise by returning response.json
.
Conclusion
To use JavaScript fetch to make DELETE and PUT requests, we can call fetch
with the method
option.