POST JSON Data with CURL

How to use CURL to post a JSON payload to a REST API endpoint?

Generic format:

$ curl -X POST -H "Content-Type: application/json" -d '{"key":"val"}' URL

Example:

$ curl -H "Content-Type: application/json" http://localhost:8000/stations --data '{"name":"Limerick3", "id":"0000001033", "region":{"name":"Shannon", "id":"2"}}'

Response data:

[{"name":"Limerick","id":"0000001043","region":{"name":"Shannon","id":2}},{"name":"Boolick","id":"0000001011","region":{"name":"Shannon","id":2}},{"name":"Limerick2","id":"","region":{"name":"Shannon","id":0}},{"name":"Limerick3","id":"","region":{"name":"Shannon","id":0}}]

Example:

$ curl -X POST -H "Content-Type: application/json" http://localhost:8000/stations --data '{"name":"Limerick4", "id":"0000001035", "region":{"name":"Shannon", "id":"2"}}'

Response data:

[{"name":"Limerick","id":"0000001043","region":{"name":"Shannon","id":2}},{"name":"Boolick","id":"0000001011","region":{"name":"Shannon","id":2}},{"name":"Limerick2","id":"","region":{"name":"Shannon","id":0}},{"name":"Limerick3","id":"","region":{"name":"Shannon","id":0}},{"name":"Limerick4","id":"","region":{"name":"Shannon","id":0}}]

More information about how to use curl can be found in the official tutorial.