How to make POST call to an API using Axios.js in JavaScript ?
Last Updated :
28 Apr, 2025
Axios is a promise-based HTTP client designed for Node.js and browsers. With Axios, we can easily send asynchronous HTTP requests to REST APIs and perform create, read, update and delete operations. It is an open-source collaboration project hosted on GitHub. It can be imported in plain Javascript or with any library accordingly.
The following script-src will include axios.js in the head section of your HTML code
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
When we send a request to the API using axios, it returns a response. The response object consists of:
- data: the data returned from the server.
- status: the HTTP code returned from the server.
- statusText: the HTTP status returned by the server.
- headers: headers obtained from the server.
- config: the original request configuration.
- request: the request object.
For the purpose of demonstration, we will be hosting an API on the localhost:
http://127.0.0.1:5000
Python Script: You will be requiring the following packages to run the API: flask, requests, jsonify, and flask_cors. The code for the Python API is as follows:
Example:
Python3
from flask import Flask, jsonify, request
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route('/test', methods =['POST'])
def test():
return jsonify({"Result": "Welcome to GeeksForGeeks, "
+request.json['name']})
if __name__ == '__main__':
app.run(debug = True)
Note: You can host this API by simply running the above python code.
JS Script: Include axios.js and the corresponding JS file in the HTML file. In the JS file, write the following code which makes a POST request using axios to the API. A POST request to the API requires the following variables:
- path: The path to the API method.
- queryObj: Query Object which contains the header data for the POST call. The query object is in the form of a Javascript object. For example: {name:'GeeksForGeeks', type:'Website'}
Example:
javascript
function makePostRequest(path, queryObj) {
axios.post(path, queryObj).then(
(response) => {
let result = response.data;
console.log(result);
},
(error) => {
console.log(error);
}
);
}
queryObj = { name: 'Chitrank' };
makePostRequest('http://127.0.0.1:5000/test', queryObj);
Output: It will call the API with a POST request carrying the mentioned header data. The response obtained will be obtained on the console window.

Similar Reads
How to Make GET call to an API using Axios in JavaScript? Axios is a promise-based HTTP client designed for Node.js and browsers. With Axios, we can easily send asynchronous HTTP requests to REST APIs and perform create, read, update and delete operations. It is an open-source collaboration project hosted on GitHub. It can be imported in plain JavaScript o
3 min read
4 Ways to Make an API Call in JavaScript API(Application Programming Interface) is a set of protocols, rules, and tools that allow different software applications to access allowed functionalities, and data, and interact with each other. API is a service created for user applications that request data or some functionality from servers.To
7 min read
How to connect to an API in JavaScript ? An API or Application Programming Interface is an intermediary which carries request/response data between the endpoints of a channel. We can visualize an analogy of API to that of a waiter in a restaurant. A typical waiter in a restaurant would welcome you and ask for your order. He/She confirms th
5 min read
How to correctly use axios params with arrays in JavaScript ? In this article, we will be going to learn about how to correctly use Axios params with arrays in Javascript. While making HTTP requests in Axios with arrays as parameters in JavaScript, one needs to serialize the array in such a way that the server can understand it easily. These are the following
2 min read
JavaScript - Wait for an API Request to Return in JS For waiting of an API we must need to use express and nodejs to run the JavaScript code. we will create a simple Node.js-based API using Express that returns a JSON response. This will demonstrate how to make API calls using Async/Await in JavaScript.Create a Simple Express APIFirst, install the req
2 min read
Manage API data and workflows using Postman JavaScript objects Managing API data and workflows efficiently is very important for developers. Postman, a popular API development environment, provides powerful tools to streamline this process. One such tool is Postman JavaScript objects, which allow you to manipulate data and automate workflows effectively. In thi
2 min read