Keywords: cURL | JSON | POST Request | HTTP Headers | Spring MVC | API Testing
Abstract: This article provides an in-depth exploration of using cURL to send POST requests with JSON data, focusing on resolving common HTTP 415 errors. By comparing incorrect and correct command formats, it explains the critical importance of Content-Type headers and demonstrates multiple approaches including direct command-line JSON submission and file-based data transmission. With Spring MVC backend code examples, the article presents complete REST API testing workflows, empowering developers to master cURL's core applications in API testing and debugging.
Fundamentals of cURL and JSON Data Transmission
cURL (Client for URLs) is a powerful command-line tool supporting data transfer across multiple protocols including HTTP, HTTPS, and FTP. In web development and API testing, cURL is commonly used to simulate client requests and validate server-side interface correctness. JSON (JavaScript Object Notation), as a lightweight data interchange format, is widely adopted in RESTful API data transmission due to its clear structure and easy parsing capabilities.
Common Error Analysis and Solutions
When using cURL to send JSON data, developers frequently encounter HTTP 415 "Unsupported Media Type" errors. This error indicates the server cannot process the media type in the request. Analyzing the original problematic command:
curl -i \
-H "Accept: application/json" \
-H "X-HTTP-Method-Override: PUT" \
-X POST -d "value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true \
http://localhost:8080/xx/xxx/xxxx
Two critical issues emerge: First, the -d parameter defaults to setting Content-Type as application/x-www-form-urlencoded, while Spring MVC's @RequestBody annotation expects application/json; Second, JSON data format requires proper quotation handling in command-line environments.
Correct cURL Command Format
To properly send JSON data, the Content-Type header must be explicitly set to application/json:
curl -H "Content-Type: application/json" \
-X POST \
-d '{"value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true}' \
http://localhost:8080/xx/xxx/xxxx
Several important details require attention: JSON data must be wrapped in single quotes to ensure proper double-quote escaping in shell environments; -H is the shorthand for --header; When using the -d parameter, -X POST can be omitted since -d implicitly defaults to POST method.
Sending JSON Data from Files
For complex JSON data structures, storing data in files and referencing them with the @ symbol is recommended:
curl -H "Content-Type: application/json" \
-d @data.json \
http://localhost:8080/xx/xxx/xxxx
This approach not only enhances command readability but also avoids complex escape character handling in command-line interfaces. The data.json file should contain complete JSON objects with proper formatting.
Spring MVC Backend Code Analysis
The corresponding Spring MVC controller code appears as:
@RequestMapping(method = RequestMethod.PUT)
public Configuration updateConfiguration(HttpServletResponse response, @RequestBody Configuration configuration) {
configuration.setName("PUT worked");
return configuration;
}
The @RequestBody annotation instructs Spring to deserialize the request body into a Configuration object, which requires the request's Content-Type to be application/json. Although the original question uses POST method with X-HTTP-Method-Override header to simulate PUT requests, the core JSON processing mechanism remains identical.
Advanced Usage and Best Practices
In newer cURL versions (7.82.0 and above), the --json option simplifies commands:
curl --json '{"username":"admin","password":"admin"}' http://localhost:5050/login
This option automatically sets Content-Type to application/json and implies POST method, reducing the need for manual header configuration. For APIs requiring authentication, combine with the -u parameter to provide credentials:
curl -H "Content-Type: application/json" \
-u username:password \
-d '{"key":"value"}' \
http://api.example.com/endpoint
Debugging Techniques and Error Handling
Using the -v (verbose) parameter reveals complete request and response details for problem diagnosis:
curl -v -H "Content-Type: application/json" -d '{"test":"data"}' http://localhost:8080/api
For Windows environments, pay attention to quotation handling differences, typically recommending PowerShell or adjusted escape rules. When encountering connection issues, checking network connectivity, port listening status, and firewall configurations are essential troubleshooting steps.
Practical Application Scenarios
cURL plays significant roles in automation scripts, CI/CD pipelines, and API documentation validation. Integrating cURL commands into shell scripts enables batch API testing:
#!/bin/bash
BASE_URL="http://localhost:8080/api"
# Test configuration creation
curl -H "Content-Type: application/json" \
-d '{"name":"test_config", "value":100}' \
${BASE_URL}/configurations
# Test configuration retrieval
curl ${BASE_URL}/configurations/1
This automated testing approach significantly enhances development efficiency and code quality.