How to Use cURL For HTTP POST Requests with JSON Data

In How to Use cURL For HTTP POST Requests with JSON Data tutorial we have explained with examples how we can use cURL to make all POST and data requests to a remote server. Before that we have explained few bits to give you an idea what is cURL and its effectiveness.

What is cURL?

Curl is a command line tool that we can use to make protocols requests including HTTP post requests. cURL was created for Linux but we can use it on Windows, Mac and BSD for the same purpose.  In this post we will use cURL to make POST requests. The HTTP POST method is used to send data to the remote server.

Post Request

We use this curl command form to make requests:

curl -X POST [options] [URL]

You should know that ‘-X’ is used to specify the HTTP request type to use for remote server requests. The request type is depicted by the Content-Type header as the post request is sent through HTML form and the data which we send to form is encoded.

Making a simple POST request

We are making a POST request to the specific URL in this example. We will specify request type using -X which is a post and will support it by usng a URL for it.

curl -X POST http://example.com

If we would not use the -X parameter the POST will not be specified and the GET method of the HTTP protocol is going to be used as a general rule:

Now we will use more fields with our POST

In the following command we are making the additional data with post request to the remote URL and specifying that data with -d as seen here:

curl -d "firstname=John&lastname=Andrew" -X POST http://example.com

We will use ‘&’ to sepratate our data generally structured data as name=value. While -d parameter adds HTTP header such as content type:

Specifying the Content-Type in the POST Request

Now when we are using Header with a curl specifying data type using -H to make a distinction. Using this command to also send JSON object with our request.

curl -d '{json}' -H 'Content-Type: application/json' https://example.com

Specifying cookies with our curl POST

Most of the web apps these days rely on cookies for authentication. We can also use curl command with a cookie to access restricted resources and also verify requests. We will use -b or -cookie to specify cookie data specification.

curl --cookie "sadad1321saweqe" -X POST http://example.com

Using curl to send files

We can use ‘@’ before our file location to post a file with curl and the that file can be in the form of an image, document etc.

curl -X POST -F 'image=@/home/user/Downloads/profile.jpg' http://example.com/upload

Using curl to send JSON data

JSON format is used for communication with curl command requests. Here we have sent JSON data to the remote server see the example:

curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST "http://example.com/data"

We can locate JSON data file like we have shown here and send its data to the server:

curl -d "@mydata.json" -X POST "http://example.com/data"

So this is how we can make cURL HTTP POST Requests with JSON Data to a remote server and also add different type of data to our post requests.