Sending POST API Request [JavaScript/AJAX Code]

To make a POST request to an API endpoint using JavaScript/AJAX, you need to send an HTTP POST request to the server and specify a Content-Type request header that specifies the data media type in the body of the POST API request. The Content-Length header indicates the data size in the POST message body. In this JavaScript/AJAX POST API Request example, we send a POST request to the ReqBin echo API URL. Click Send to execute the JavaScript/AJAX POST API Request online and see results. The JavaScript/AJAX code was automatically generated for the Sample API POST Request example.
Sending POST API Request [JavaScript/AJAX Code] Send
POST /sample/post/json HTTP/1.1
Host: reqbin.com
Content-Type: application/json
Content-Length: 0

Updated: Viewed: 64471 times
JavaScript/AJAX code for Sample API POST Request example

JavaScript/AJAX code for Sample API POST Request Example

This JavaScript/AJAX code snippet was generated automatically for the Sample API POST Request example.
<< Back to the Sample API POST Request example

What is API?

API (Application Programming Interface) is a program mediator that allows two applications to exchange data and complies with HTTP and REST standards. API refers to standards and documentation that describe how to create or use such a connection or interface. The primary purpose of APIs is to hide the internal details of how the system works and show only those parts that are visible and accessible to external systems, keeping them consistent even if the internal implementation change later.

What is POST request?

The HTTP POST request method sends data to the server. For CRUD operations, the HTTP POST method is used to create or update a resource on the server. The POST is one of the most widely used methods of the HTTP protocol. The HTTP POST method is used to submit web forms, upload files and images to the server.

How to make a POST request to an API?

The following is an example of sending a POST API request to ReqBin echo URL:

API POST Example
POST /sample/post/json HTTP/1.1
Host: reqbin.com
Content-Type: application/json
Content-Length: 17
      
{
  "Id": 78912
}

Server response to our Sample API POST Request:

API POST Example
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 19
Connection: keep-alive

{"success":"true"}

See also

Generate code snippets for JavaScript/AJAX and other programming languages

Convert your Sample API POST Request request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the JavaScript/AJAX code generator.