Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Empowering you to understand your world

How To Send A POST Request With JavaScript

By Nicholas Brown.

You can send a POST request with JavaScript using the ‘XMLHttpRequest’ class using few lines of code. There are many uses for JavaScript POST requests, including sending data from a form or other HTML element to a server for processing or storage. A POST request differs from a GET request because it has a body. A GET request encodes the parameters it needs to send in the URL of the request because it has no body.

You can insert an entire JSON object or JSON array in the body of your POST request as shown in the example below. To send a JavaScript POST request, ensure that you have sorted out and decided on the following details first:

  • The URL of the endpoint you will send the request to.
  • The data you will insert into the body of the request before sending it off. Example: A record containing the details of a shipment or details about a phone you’re selling in a store. A phone is used in the example below.
  • The data type of the body, which could be JSON or a plain string.
  • The purpose of the request. For this example, you will be entering a phone that you intend to sell in your inventory database. Learn how to store data in a MongoDB database and a PostgreSQL database.

JavaScript POST Request Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        const url = "http://localhost:8080/testendpoint";
        let request = new XMLHttpRequest();
        let newParagraph = document.createElement("p");
        request.onreadystatechange = function() {
            if (request.readyState === 4 && request.status === 200) {
                let apiresponse = request.responseText;
                let text = document.createTextNode(apiresponse);
                newParagraph.appendChild(text);
                newParagraph.innerText = apiresponse;
                document.body.appendChild(newParagraph);
            }
        };
        request.open("POST", url, true);
        request.setRequestHeader("Content-Type", "application/json");
        request.send( JSON.stringify({"Brand": "Apple", "Model": 'iPhone 14 Pro Max', "Price": '$1,200', "Storage": "512GB", "Colour": "Graphite"}) );
    </script>
</head>
<body>
</body>
</html>

The request in this example is being sent to a server on localhost (which would be the machine this app is run on). You can set up a test server to try it out using my Node.js POST request tutorial. Change ‘url’ to whichever server you are going to send your request to. You could also use Request Catcher.

The code block following ‘request.onreadystatechange = function()’ creates a paragraph in an HTML page to display the server’s response for test purposes. It isn’t required for this JavaScript POST request tutorial.

‘request.open(“POST”, url, true);’ indicates to the method that you want to send a POST request, and the URL you assigned to the ‘url’ variable above is where it will send the request.

‘request.setRequestHeader(“Content-Type”, “application/json”);’ is necessary to tell the server that you are sending it a JSON object. This is enables the server to determine how to go about parsing it.

‘request.send()’ takes the request body as a parameter. This is where you will place your JSON object.

Further Reading

Send A GET Request Using JavaScript

Subscribe to our newsletter
Get notified when new content is published