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 flat string. Double quotes in JSON must be escaped with the backslash \. Click Run to execute Curl POST JSON example online and see results. This will post JSON to the ReqBin echo URL.
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 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, to send JSON using the POST method, you need to use the -X POST command-line parameter.
Why do I need to explicitly specify the Content-Type when posting JSON using Curl?
If you are submitting data using Curl and you do not explicitly specify the content type, Curl uses the application/x-www-form-urlencoded content type for your data. Therefore, when you send JSON (or any other data type), you need to explicitly 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, if you don't set the correct Content-Type, your application may not work.
The Content-Type header field indicates the media type included in the 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, then 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.