Need to test your API by sending JSON data from a file? Here are the curl commands.
POST JSON File to Localhost
Command
curl -X POST http://localhost:8080/api/endpoint \
-H "Content-Type: application/json" \
-d @data.json
Replace:
8080with your port/api/endpointwith your API pathdata.jsonwith your JSON file path
The @ symbol tells curl to read from file.
PUT JSON File
Command
curl -X PUT http://localhost:8080/api/endpoint \
-H "Content-Type: application/json" \
-d @data.json
Same as POST, just using PUT method.
One-Liner Version
Command
curl -X POST http://localhost:8080/api/endpoint -H "Content-Type: application/json" -d @data.json
Everything on one line.
Show Response Headers
Command
curl -X POST http://localhost:8080/api/endpoint \
-H "Content-Type: application/json" \
-d @data.json \
-i
-i includes response headers in output.
Verbose Output (Debug)
Command
curl -X POST http://localhost:8080/api/endpoint \
-H "Content-Type: application/json" \
-d @data.json \
-v
-v shows full request and response details.
With Authentication
Basic Auth:
Command
curl -X POST http://localhost:8080/api/endpoint \
-H "Content-Type: application/json" \
-u username:password \
-d @data.json
Bearer Token:
Command
curl -X POST http://localhost:8080/api/endpoint \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-token-here" \
-d @data.json
API Key:
Command
curl -X POST http://localhost:8080/api/endpoint \
-H "Content-Type: application/json" \
-H "X-API-Key: your-api-key" \
-d @data.json
Send Inline JSON (No File)
Command
curl -X POST http://localhost:8080/api/endpoint \
-H "Content-Type: application/json" \
-d '{"key":"value","number":123}'
Single quotes prevent shell interpretation.
File in Different Directory
Command
curl -X POST http://localhost:8080/api/endpoint \
-H "Content-Type: application/json" \
-d @/path/to/data.json
Use absolute or relative path.
Save Response to File
Command
curl -X POST http://localhost:8080/api/endpoint \
-H "Content-Type: application/json" \
-d @data.json \
-o response.json
Response saved to response.json.
Common Localhost Ports
- 3000: Node.js, React dev servers
- 8000: Python HTTP server, Django
- 8080: Common Java, general dev
- 5000: Flask default
- 4200: Angular dev server
- 3001: Alternative Node.js
Troubleshooting
"Failed to connect" error:
- API not running
- Wrong port number
- Check
http://vshttps://
"curl: no such file" error:
- JSON file doesn't exist
- Wrong file path
- Check current directory with
pwd
Unexpected response:
- Check JSON syntax with
cat data.json - Validate JSON at jsonlint.com
- Use
-vflag to see full request
Example JSON File
Create data.json:
Command
{
"name": "Test User",
"email": "test@example.com",
"active": true,
"roles": ["admin", "user"]
}
Then run:
Command
curl -X POST http://localhost:8080/users -H "Content-Type: application/json" -d @data.json
Bottom Line
Basic POST command:
Command
curl -X POST http://localhost:8080/api/endpoint -H "Content-Type: application/json" -d @data.json
The @ symbol reads from file. Change POST to PUT for PUT requests. Add -v for debugging. Add -i for response headers.
Works with any localhost API during development.