cURL - Post JSON data

By xngo on September 26, 2019

cURL is a command-line tool for transferring data using various protocols. It can be used to make requests to RESTful web services. In this tutorial, I will show you how to post JSON data using cURL.


Assuming that you have a web service(http://localhost/create.php) that accepts the following JSON format:

    "name": "some string",
    "role": "some string"

Post JSON string

To post your JSON data as string, run cURL as follows:

curl -X POST -H "Content-Type: application/json" \
    -d '{ "name": "Joe Smith", "role": "CEO" }' http://localhost/create.php

This way of posting JSON data will be a headache when you have to deal with single and double quotes. You need to escape them. The better approach would be to post JSON data from a file. See the section below.

Post JSON file

To post your JSON data from a file, simply add your data in a file, e.g. data.json. And, then, run cURL as follows:

curl -X POST -H "Content-Type: application/json" \
    -d @data.json  http://localhost/create.php

About the author

Xuan Ngo is the founder of He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.