POST JSON data with CURL example

This article guides you how to post JSON data with cURL. You’ll learn the syntax and see several practical examples that demonstrate how to use this technique. When working with APIs or web services, you may frequently need to send JSON data via POST requests. cURL, a powerful command-line tool, can be used for this purpose.

POST JSON data with CURL

Table of Contents


cURL Syntax for POSTing JSON Data

To POST JSON data with cURL, you’ll need to use the following syntax:

  curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' URL

Let’s break down the components:

  • -X POST instructs cURL to use the POST method
  • -H adds an HTTP header. "Content-Type: application/json" specifies the media type as JSON.
  • -d followed by JSON data in single quotes is the data payload you want to send.
  • URL is the endpoint you are sending the request to.


Practical Examples

Below are some practical examples that show how to post JSON data with cURL to various kinds of APIs:

Example 1: Simple JSON POST

  curl -X POST -H "Content-Type: application/json" -d '{"username":"myuser", "password":"mypassword"}'


  {"status":"success", "message":"Logged in successfully"}

This command sends a login request to the URL specified with a username and password as JSON data.


Example 2: POST with Bearer Token Authentication

  curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer YourTokenHere" -d '{"post":"This is a test post"}'


  {"status":"success", "message":"Post created"}

Here, an additional Authorization header is included to fulfil the authentication requirement, while posting data.


Example 3: Posting an Array of Data

  curl -X POST -H "Content-Type: application/json" -d '[{"product":"Book", "quantity":"2"}, {"product":"Pen", "quantity":"10"}]'


  {"status":"success", "message":"Order placed"}

This example demonstrates how to send an array of objects as JSON data as part of POSTing multiple items in an order.


Example 4: Using cURL with a Data File

If your JSON payload is large, you can store it in a file and use cURL to send it:

  curl -X POST -H "Content-Type: application/json" -d @data.json

The @ symbol is used to specify a file containing the JSON data to be sent. Ensure that the file data.json exists with valid JSON.


Example 5: Verbose Output

  curl -v -X POST -H "Content-Type: application/json" -d '{"status":"active"}'

Adding the -v (verbose) option shows the request and response headers which help in debugging.


Conclusive Summary

In this tutorial, you’ve learned how to post JSON with cURL through a variety of examples each tailored to specific scenarios. Remember that when creating a cURL command, it is essential to set the correct Content-Type header and to properly format your JSON data. Whether you’re dealing with simple JSON structures, arrays, or authentication, cURL offers the flexibility to handle your data transfer needs with ease. Practice these commands to become proficient in API communication with cURL.