How to Send an HTTP POST Request in JS? Last Updated : 21 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report We are going to send an API HTTP POST request in JavaScript using fetch API. The FetchAPI is a built-in method that takes in one compulsory parameter: the endpoint (API URL). While the other parameters may not be necessary when making a GET request, they are very useful for the POST HTTP request. The second parameter is used to define the body (data to be sent) and type of request to be sent, while the third parameter is the header that specifies the type of data you will send, for example, JSON. The Fetch API is based on JavaScript promises, so you need to use the .then method to access the promise or response returned. There are several methods to send an HTTP POST request in JavaScript which are as follows: Table of Content Using Fetch Using Ajax( XMLHttpRequest)Using Fetch In this approach, the body holds the data to be sent to the server which is added to the JSON Placeholder todos API. Also, the headers hold the type of content you want to send to the server, which in this case is JSON data. Example: To demonstrate sending an HTTP POST request using the fetch method in JavaScript. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> </head> <body> <script> fetch("https://jsonplaceholder.typicode.com/todos", { method: "POST", body: JSON .stringify ({ userId: 1, title: "Demo Todo Data", completed: false, }), headers: { "Content-type": "application/json", }, }) .then((response) => response.json()) .then((json) => console.log(json)); </script> </body> </html> Output: Sending HTTP POST request in JavaScript using the Fetch method in JavaScript.Using Ajax( XMLHttpRequest) Ajax XMLHttpRequest is a in-built function and has existed much longer than the Fetch API. This means that almost all modern browsers have a built-in XMLHttpRequest object to request data from a server. Example: To demonstrate sending an HTTP POST request in JavaScript. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> </head> <body> <script> const xhr = new XMLHttpRequest(); xhr .open("POST", "https://jsonplaceholder.typicode.com/todos"); xhr .setRequestHeader("Content-Type", "application/json"); const body = JSON .stringify( { userId: 1, title: "Demo Todo Data using XMLHttpRequest", completed: false, }); xhr .onload = () => { if (xhr.readyState == 4 && xhr.status == 201) { console.log(JSON.parse(xhr.responseText)); } else { console.log(`Error: ${xhr.status}`); } }; xhr.send(body); </script> </body> </html> Output: HTTP post request in JavaScript Comment More infoAdvertise with us Next Article How to create and send POST requests in Postman? B bishal1289be21 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How Are Parameters Sent In An HTTP POST Request? HTTP POST requests are widely used in web development to send data from a client to a server. Whether you're submitting a form, uploading a file, or sending JSON data via an API, understanding how parameters are sent in an HTTP POST request is important. In this article, weâll explore how are parame 4 min read How to send a POST Request with PHP ? In web development, sending POST requests is a common practice for interacting with servers and exchanging data. PHP, a versatile server-side scripting language, provides various approaches to accomplish this task. This article will explore different methods to send POST requests using PHP. Table of 3 min read How HTTP POST requests work in Node ? The HTTP POST method is used to send data from the client to the server. Unlike GET, which appends data in the URL, POST sends data in the request body, which makes it ideal for form submissions, file uploads, and secure data transfers.In Node.js, handling POST requests is commonly done using the Ex 2 min read How to create and send POST requests in Postman? Postman is an API(application programming interface) development tool which helps to build, test and modify APIs. It can make various types of HTTP requests(GET, POST, PUT, PATCH), saving environments for later use, and convert save the API to code for various languages(like JavaScript, and Python). 2 min read How to Handle a Post Request in Next.js? NextJS is a React framework that is used to build full-stack web applications. It is used both for front-end as well as back-end. It comes with a powerful set of features to simplify the development of React applications. In this article, we will learn about How to handle a post request in NextJS. A 2 min read How to send REST response to html in Angular ? In this article, we will see how to send API responses using HttpClient Module to an HTML using Angular, along with understanding the basic implementation through the examples.Angular is a JavaScript framework through which we can create reactive single-page web applications. For implementation, we 3 min read Like