Axios: how to formulate a Delete request via header and body?

When your website or application needs to communicate with an API, it uses the HTTP protocol for this. This protocol, which is the basis of internet communications, uses a system of methods.

Axios: how to formulate a Delete request via header and body?

When your website or application needs to communicate with an API, it uses the HTTP protocol for this. This protocol, which is the basis of internet communications, uses a system of methods. If you have created a website, you are already familiar with the GET method, which passes parameters in the URL, and the POST method, which positions these parameters in the body. They are used in forms. There are also other methods that are used with APIs.

For example, there is PUT, which is designed to indicate that we are updating an entire resource, or PATCH for a partial update. 

The DELETE method is used to indicate that you want to delete a resource. To indicate the resource to be deleted, its identifier must be specified as a parameter. 

We can do all this with the Axios library in the JavaScript language. 

The "delete()" method is specifically designed for HTTP requests of this type. It accepts the API URL as the first parameter. As a second parameter, you can give it options in the form of an object. It is this object that will contain the data. The "data" attribute allows you to specify the parameters used to identify the resource. Then enter the name of the object followed by its value. To perform this operation, you will need to authenticate with the API. To do this, you must specify the "Authorization" header in the options. It is the "headers" attribute that contains the headers of the request.

axios.delete(http://monsite.fr/api, { 
data: { id: "resource-id" },
 headers: { "Authorization": "authorization key" }
 });


What's Your Reaction?

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0