9 Tweets 9 reads Jun 15, 2022
How to use Axios to make API requests?
Thread ๐Ÿงต๐Ÿ‘‡
Axios is a library used for making HTTP requests. It has a Promise-based API that makes it easy to use with JavaScript.
In order to include Axios, you can install it as a package or use CDN.
Axios includes several predefined methods you can use to perform CRUD operations.
Let's say we want to create a POST request and send some user-specific data to the server.
For this, we can use a POST method included in Axios.
As you can see you'll receive data back in the "then" method.
You should know two things here.
1. If you send JSON data Axios will automatically serialize them. No need to explicitly serialize them.
2. Axios will automatically set the Content-Type header to application/json to indicate that the data is in JSON format.
You can see the structure of the response object below:
- data - contains the actual data returned by the API
- status - HTTP status code
- statusText - HTTP status message
- headers - headers returned by the server
- config - config that was sent with the request
If you want to send custom headers with requests here is how you can do so.
We can use the third parameter of the POST method for this purpose which contains the config for the request.
Thanks for reading!
Follow us @Rapid_API for more exclusive content. ๐Ÿ™๐Ÿ’™

Loading suggestions...