Sending JSON with Curl [JavaScript/AJAX Code]

To send JSON data to the server using Curl, you need to pass the Content-Type: application/json HTTP header with the -H command line argument and the JSON data with the -d (or --data) command line argument. JSON data is passed as a string. You may also need to send the -H "Accept: application/json" HTTP header, which informs the server that the Curl client expects a JSON response. If you omit the Accept header, the server may respond with data of a different MIME type than JSON. The response Content-Type header specifies the type of data returned by the server. In this Curl POST JSON data example, we send JSON to the ReqBin echo URL. Click Run to execute the Curl JSON Request Example online and see the result. The JavaScript/AJAX code was automatically generated for the Curl JSON Request example.
Sending JSON with Curl [JavaScript/AJAX Code] Run
curl -X POST https://reqbin.com/echo/post/json
     -H "Content-Type: application/json"
     -H "Accept: application/json" 
     -d '{"Id": 7,"Customer":"Leo"}'
Updated: Viewed: 19293 times
JavaScript/AJAX code for Curl JSON Request example

JavaScript/AJAX code for Curl JSON Request Example

This JavaScript/AJAX code snippet was generated automatically for the Curl JSON Request example.
<< Back to the Curl JSON Request example

What is Curl?

Client for URLs (Curl) is a popular command-line tool for Linux, Windows, and macOS that is commonly used for transferring files over networks using HTTP, HTTPS, FTP, or SFTP protocols. You can make GET, POST, and HEAD requests to the server, retrieve HTTP headers, download HTML pages, upload files, submit forms, and more.

What is JSON?

JavaScript Object Notation (JSON) is a textual data interchange format based on JavaScript. In the same way, as many other textual formats, JSON is easily readable by humans and parsed by computers. Despite its origins in JavaScript, this format is considered language-independent and compatible with most programming languages. For many programming languages, there is ready-made code for creating and manipulating JSON data.

JSON Example
{
  "Id": 78912,
  "Customer": "Jason Sweet",
  "Quantity": 1,
  "Price": 18.00
}

Why to need to specify Content-Type when sending JSON with Curl?

If you do not explicitly specify the Content-type when submitting data using Curl, Curl uses the application/x-www-form-urlencoded content type. The -H "Content-Type: application/json" command line parameter must be specified when sending JSON (or any other data type).

How to send JSON with Curl?

The following is an example of sending JSON to Curl:

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

See also

Generate code snippets for JavaScript/AJAX and other programming languages

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