Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to make POST call to an API using Axios.js in JavaScript ?

  • Last Updated : 21 Jul, 2021

Axios is a promise-based HTTP client designed for Node.js and browser. 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=""></script>

When we send a request to the API using axios, it returns a response. The response object consists of: 

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • 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:

Python Script: You will be requiring the following packages to run the API: flask, requests, jsonify, flask_cors. The code for the Python API is as follows:

  • Program: 


from flask import Flask, jsonify, request
from flask_cors import CORS
app = Flask(__name__)
@app.route('/test', methods =['POST'])
def test():
   return jsonify({"Result": "Welcome to GeeksForGeeks, "
if __name__ == '__main__': = 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 contain the header data for the POST call. The query object is in the form of a Javascript object. For example: {name:’GeeksForGeeks’, type:’Website’}


  • Program: 


function makePostRequest(path, queryObj) {, queryObj).then(
        (response) => {
            var result =;
        (error) => {
queryObj = { name: 'Chitrank' };
makePostRequest('', 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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!