cURL is a powerful command-line tool used for making HTTP requests, commonly used for sending and receiving JSON data in APIs. Whether you're working with REST APIs, automation scripts, or debugging requests, understanding how to send JSON data with cURL is essential.
This guide walks you through various ways to work with cURL and JSON, including how to post JSON, send a JSON body, and even upload a JSON file with cURL.
Sending JSON from a File
When you're dealing with structured data, it's often more convenient to save your JSON in a file and send it directly. With cURL, you can do this easily using the @ symbol followed by the filename. This is especially useful for larger payloads or cases where you need to reuse the data in multiple requests.
-X POST: Specifies the HTTP method (POST in this case)
-H "Content-Type: application/json": Sets the HTTP header indicating that sent data is of JSON type
-d @data.json: Specifies the data to send where @ tells curl to read the data from a local file instead of a direct string
In the above command, data.json is the file containing the JSON payload. cURL reads the file and sends its contents as the request body. This method is useful for sending large or complex JSON structures without embedding them directly in the command.
Using cURL to post a JSON file keeps your request clean and manageable, making it a great approach for automation scripts and API testing.
Now that we’ve covered sending JSON from a file, let's explore how to send it directly from a string.
Sending JSON from a String
For quick API requests, you can send JSON directly as a string using the -d flag. This method is perfect for smaller payloads or when you’re just testing an API.
Here, we specify the JSON data inline within the -d option. This method works well for quick API requests but can become cumbersome with longer JSON structures.
This inline approach makes it easy to post JSON with cURL without needing a separate file, which is handy for debugging and quick interactions with APIs.
Sometimes, JSON is generated dynamically from other commands. Let’s see how that works next.
Sending JSON from Pipe
Piping JSON between commands is a powerful way to dynamically generate, transform, and send data. Here are comprehensive examples demonstrating different piping scenarios:
# Generate JSON with jq and pipe directly to curl
$ echo '{"users": [{"name": "Alice"}, {"name": "Bob"}]}' | jq '.users[0]' | curl -X POST https://httpbin.dev/post \
-H "Content-Type: application/json" \
-d @-
The key points here:
echo creates initial JSON
jq filters or transforms the JSON
curl -d @- reads data from stdin (the pipe)
Sending JSON from Inline Command
Generating JSON dynamically is useful in scripting and automation. A common tool for this is jq, which formats JSON output.
$ curl -X POST https://httpbin.dev/post \
-H "Content-Type: application/json" \
-d "$(jq -n --arg name "Alice" --argjson age 30 '{name: $name, age: $age}')"
Here’s what happens in the command above:
jq -n starts a new JSON object.
--arg and --argjson insert variables into the JSON.
The output of jq is used as the -d parameter for cURL.
This approach is useful when dynamically constructing JSON from scripts or fetching data from other sources.
Now, let’s see how to use environment variables in JSON payloads.
Using Environment Variables in JSON
Using environment variables helps make commands more dynamic and reusable. Here's an example:
In this example $NAME and $AGE are substituted with their values and JSON is passed as a string with embedded variables.
This method is particularly useful in scripts where values change dynamically.
Before sending JSON, it's a good idea to validate it. Let's discuss that next.
Tip: Validate JSON Before Sending
To ensure your JSON is correctly formatted before sending, you can use jq or cURL with httpbin.dev for validation:
$ cat data.json | jq empty
If the JSON is invalid, jq will return an error. Alternatively, test the JSON by making a POST request to https://httpbin.dev/post, which echoes the received data:
After aquiring the api key we can now try the api using cURL
$ curl -X POST "https://translation.googleapis.com/language/translate/v2?key=YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"q": "Hello, how are you?",
"source": "en",
"target": "es",
"format": "text"
}'
This request translates "Hello, how are you?" from English to Spanish.
Next, let’s see how to send JSON messages to Slack.
Sending JSON to Slack Messages
Slack provides multiple methods to programmatically send messages to channels and users. The two most common approaches are through the Web API using authentication tokens and through Incoming Webhooks.
Sending a Message with API Token
The API approach offers more flexibility with advanced formatting options like blocks, attachments, and interactive elements.
cURL is a powerful command-line tool for making HTTP requests, but it’s not always the most user-friendly option. Several alternatives offer better usability or additional features. Here are some top options to consider.
Curlie
Curlie offers cURL’s power with ease of use. It supports both command styles and provides improved default settings for a better developer experience.
Pros:
Enhanced readability with formatted output
Works with both cURL and HTTPie syntax
Useful for quick API testing
Cons:
Slight learning curve for cURL users
Not as feature-rich as raw cURL for advanced cases
Postman is a GUI-based API testing tool that simplifies complex API interactions. It provides a visual request builder, variable management, and automation features, making it ideal for developers working with APIs.
Curl Impersonate is a modified version of cURL designed for web scraping and bypassing anti-bot protections. It mimics browsers like Chrome and Firefox, helping requests appear more legitimate.
Pros:
Reduces chances of detection when scraping websites
Supports browser-like TLS and HTTP2 fingerprinting
Each tool has unique strengths, so the best choice depends on your needs. If you prefer a GUI, Postman might be best. For command-line work, Curlie, or Curl Impersonate are solid choices.
FAQ
Below are quick answers to common questions about sending JSON with cURL.
How do I send JSON with cURL?
You can send JSON using the -d option with -H "Content-Type: application/json", either from a file (@data.json) or inline ('{"key": "value"}').
Can I send JSON with a GET request in cURL?
No, GET requests typically do not support a request body. However, some APIs may allow it, though it is not standard practice and not valid as per the HTTP protocol.
How do I validate JSON before sending it?
You can use jq empty to check if the JSON is well-formed, or send a test request to
https://httpbin.dev/post to confirm its structure.
Summary
In this guide, we covered various ways to send JSON using cURL:
Sending JSON from a file or inline string using the -d option.
Setting and using environment variables for dynamic values in curl requests.
Generating JSON with jq before sending it off.
Using JQ to validate JSON before sending it.
Real-world curl JSON post examplers like Google Translate API and Slack messages.
With these techniques, you can confidently send JSON data using cURL in any API workflow.