Posting JSON with Curl [PHP Code]

To post JSON data using Curl, you need to set the Content-Type of your request to application/json and pass the JSON data with the -d command line parameter. The JSON content type is set using the -H "Content-Type: application/json" command line parameter. JSON data is passed as a string. Double quotes in JSON must be escaped with the backslash "\" on Windows computers. In this Curl POST JSON example, we send JSON to the ReqBin echo URL. Click Run to execute the Curl POST JSON example online and see result. The PHP code was automatically generated for the Curl POST JSON example.
Posting JSON with Curl [PHP 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: 104376 times
PHP code for Curl POST JSON example

PHP code for Curl POST JSON Example

This PHP code snippet was generated automatically for the Curl POST JSON example.
<< Back to the Curl POST JSON example

What is Curl?

Curl is a powerful command line tool used to send and receive data over various protocols using URL notation. Curl supports a wide range of protocols, including HTTPS, HTTPS, FTP, SFTP, and many others. Curl is designed to run smoothly on various platforms and architectures, from Linux and Windows to macOS and beyond.

What is JSON?

JSON (JavaScript Object Notation) is a lightweight text format designed to encapsulate structured data, based on the syntax of the JavaScript. JSON, primarily used in server-client communications, has become the most commonly used format for transmitting data over the Internet in web and mobile applications, due to its simplicity and ease of use.

What is HTTP POST?

POST is one of the most widely used methods of the HTTP protocol. The POST method requests the webserver to receive and process the data enclosed in the body of the POST message. The POST method is often used to upload files and submit HTML forms.

How to make a POST request with Curl?

There are two ways to send a POST request with Curl.

  1. When you use command-line parameters such as --data or --form and do not explicitly specify the required HTTP method, Curl automatically selects the POST method and sends a POST request with the application/x-www-form-urlencoded content type (or multipart/form-data for --form).
  2. Explicitly specify the required HTTP method using the -X command-line argument. For example, you can use the -X POST-command-line parameter to send JSON using the POST method.

Why do I need to explicitly specify the Content-Type when posting JSON using Curl?

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

Why is it important to specify the correct Content-Type when submitting JSON?

In short, your server may not work properly if you don't set the correct Content-Type. The Content-Type header indicates the media type included in the POST message payload. The specified media type determines both the format of the data and the way the server handles that data.

For example, if the server can accept XML and JSON data on the same API endpoint, setting the Content-Type to application/json will let the server know that the client is sending JSON data, and application/xml will tell the server that the client is sending XML.

Curl POST Request Syntax

The general form of a Curl command for making a POST request with a JSON body is as follows:

Curl POST Request with JSON
curl -X POST [URL]
     -H "Content-Type: application/json" 
     -d "[JSON data]" 

Where:
  • -X, --request: HTTP method to use when communicating with the server.
  • -H, --header: HTTP headers to send to the server with a POST request.
  • -d, --data: Data to be sent to the server using a POST request.

Curl POST JSON Examples

The following are examples of sending JSON to Curl:

Posting JSON with Curl

The following is an example of sending JSON data to the ReqBin echo URL:

Curl POST JSON Example
curl -X POST https://reqbin.com/echo/post/json
     -H 'Content-Type: application/json' 
     -d '{"name":"Leo","age":26}'

Posting JSON file with Curl

To post a JSON file using Curl, you can pass the filename in the -d command line parameter after the "@" symbol:

Curl POST JSON File Example
curl -X POST https://reqbin.com/echo/post/json -d @filename

Posting JSON with special characters

To send a string of JSON data with special characters, you must escape the characters in the string so your server can process them correctly.

Curl POST JSON with Special Characters Example
curl -X POST https://reqbin.com/echo/post/json
     -H 'Content-Type: application/json'
     -d '{"message":"Hello World!","special_characters":"\/\r\n\t"}'

See also

Generate code snippets for PHP and other programming languages

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