When sending data via a POST or PUT request, two common formats (specified via the Content-Type header) are:
application/jsonapplication/x-www-form-urlencoded
Many APIs will accept both formats, so if you're using curl at the command line, it can be a bit easier to use the form urlencoded format instead of json because
- the json format requires a bunch of extra quoting
- curl will send form urlencoded by default, so for json the
Content-Typeheader must be explicitly set
This gist provides examples for using both formats, including how to use sample data files in either format with your curl requests.
For sending data with POST and PUT requests, these are common curl options:
-
request type
-X POST-X PUT
-
content type header
-
-H "Content-Type: application/x-www-form-urlencoded" -
-H "Content-Type: application/json" -
data
- form urlencoded:
-d "param1=value1¶m2=value2"or-d @data.txt - json:
-d '{"key1":"value1", "key2":"value2"}'or-d @data.json
- form urlencoded:
application/x-www-form-urlencoded is the default:
curl -d "param1=value1¶m2=value2" -X POST http://localhost:3000/data
explicit:
curl -d "param1=value1¶m2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/data
with a data file
curl -d "@data.txt" -X POST http://localhost:3000/data
curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/data
with a data file
curl -d "@data.json" -X POST http://localhost:3000/data