Converting Curl Commands to JavaScript/AJAX Code

You can convert Curl commands to JavaScript/AJAX requests using the ReqBin Online Curl client. The Curl to JavaScript Converter uses the vanilla JavaScript XMLHttpRequest calls for the generated code. Run and test your Curl commands online with the online ReqBin Curl Client and convert them to JavaScript code when ready. Curl Converter automatically generates valid AJAX calls for all provided HTTP headers, data, and forms.
Converting Curl Commands to JavaScript/AJAX Code Run
curl -X POST https://reqbin.com/echo/post/json
	 -H "Content-Type: application/json" 
     -d "{\"login\":\"my_login\",\"password\":\"my_password\"}"
Updated: Viewed: 18114 times

What is Curl?

Curl is a command-line tool with a cross-platform libcurl library for sending HTTP requests from clients to servers using over 25+ protocols, including HTTP, HTTPS, and FTP. Curl is used for debugging network requests and API calls and has built-in support for proxy, web forms, SSL, HTTP Cookies, certificate validation and works on all modern platforms such as Linux, Windows, and macOS.

What is JavaScript/AJAX?

JavaScript AJAX stands for Asynchronous JavaScript and XML. AJAX is used on the client-side (in a web browser) to create asynchronous interactive web applications. JavaScript can use AJAX calls to send and receive data in a variety of formats, including JSON, XML, and HTML, communicate with the server, and refresh the page without reloading the whole web page.

JavaScript AJAX Request Example

To make an asynchronous HTTP requests to the server using JavaScript, you need to instantiate the XMLHttpRequest object, make the request, and listen for the onreadystatechange event.

JavaScript AJAX Code Example
let url = "https://reqbin.com/echo/get/json";

let xhr = new XMLHttpRequest();
xhr.open("GET", url);

xhr.onreadystatechange = function () {
   if (xhr.readyState === 4) {
      console.log(xhr.status);
      console.log(xhr.responseText);
   }};

xhr.send();

The following is an example of converting Curl request to JavaScript/AJAX code:

Curl Command Example
curl -X POST https://reqbin.com/echo/post/json
   -H "Content-Type: application/json" 
   -d "{\"login\":\"my_login\",\"password\":\"my_password\"}"

The following is an example of generated JavaScript/AJAX code:

JavaScript/AJAX Code Example
let url = "https://reqbin.com/echo/post/json";

let xhr = new XMLHttpRequest();
xhr.open("POST", url);

xhr.setRequestHeader("Content-Type", "application/json");

xhr.onreadystatechange = function () {
   if (xhr.readyState === 4) {
      console.log(xhr.status);
      console.log(xhr.responseText);
   }};

let data = '{"login":"my_login","password":"my_password"}';

xhr.send(data);

See also