- On the server-side it uses the native Node.js http module.
- On the client-side (browser) it uses XMLHttpRequests. Additionally, there are important features that you should know:.
- Supports the Promise API. Intercept request and response (Axios Interceptors tutorial).
Transform request and response data. Cancel requests. Automatic transforms for JSON data. Client side support for protecting against XSRF. (from https://github.com/axios/axios#features). We can add Axios to our project/code with one of following simple ways:.
The response for a Axios request contains:. data: parsed response body provided by the server.
status: HTTP status code. statusText: HTTP status message. headers: HTTP headers (lower case).
config: the request config that was provided to axios. request: the last client request instance that generated this response. Axios Response Object has data field that contains the parsed response body.We can use then or await to receive the response body as follows:.
We use catch() for handling errors. If you want to use async-await, just wrap the axios call with try/catch block.
You can use the params config option to set query string params.
And this is equivalent:. To send Axios GET request with Headers, we pass an option object with headers property.
We can merge params and headers in a Axios GET request like this. You can perform an Axios POST object request with body as second parameter.
To send Axios POST request with Headers, we pass an option object with headers property right after the body. You can perform an Axios PUT json object request with body as second parameter.
To send Axios PUT request with Headers, we pass an option object with headers property right after the body. To send Axios DELETE request with Headers, we pass an option object with headers property.
We can create a new instance of axios using axios.create(config) method.
We will build a HTTP Client to make CRUD requests to Rest API in that:. Axios GET request: get all Tutorials, get Tutorial by Id, find Tutorial by title.
Axios POST request: create new Tutorial. Axios PUT request: update an existing Tutorial.
Axios DELETE request: delete a Tutorial, delete all Tutorials. This Axios Client works with the following Web API:.
You can find step by step to build a Server like this in one of these posts:.
Remember that you need to configure CORS: Access-Control-Allow-Origin: *.It helps the REST APIs can be accessed by any origin.
Create a Tutorial using axios POST request:.
Retrieve all Tutorials using axios GET request:.
Retrieve a Tutorial by Id using axios GET request:. Find Tutorials by title using axios GET request with params:.