Sending Data with POST Requests
Here we make use of async and await syntax which are methods under Promises API. They help in writing cleaner and manageable code that is easier to interpret and gives a synchronous feel. Any request object can be sent in the similar fashion and this is how axios is incorporated into the code using a wrapper function.
Let’s take an example of a login request where we are sending username and example as the request object:
const loginData = async (credentials) => {
try {
const response = await axios.post(url, credentials)
console.log(response.data);
} catch (error) {
console.log(error);
}
};
loginData({
username: "Finn",
password: "fin237r87@#"
});
How To Use Axios NPM to Generate HTTP Requests ?
In this article, we are going to learn about Axios and HTTP requests and using Axios to generate HTTP requests. Axios is a promise-based HTTP library that is used by developers to make requests to their APIs or third-party API endpoints to fetch data. It is a popular JavaScript library used for making HTTP requests from web browsers and NodeJS applications. It provides a simple and intuitive API for performing asynchronous operations like fetching data from APIs, posting form data, and more.
Table of Content
- HTTP Requests
- Axios
- How does Axios work?
- Installing Axios NPM for Your Project
- Creating an Axios Instance With Default Settings
- Performing GET Requests With Axios
- Performing POST Requests With Axios
- Sending Data with POST Requests
- Handling Response Data From POST Requests
- Shorthand Methods for Axios HTTP Requests
- Error Handling With Axios
- Conclusion