These curl recipes show you how to add POST data to curl requests. To add URL-encoded form data, use the -d argument. To make curl URL-escape form data for you, use the --data-urlencode argument. To send multipart form data, use the -F argument.

Send Form Data

curl -d 'name=johnny%20depp' https://google.com/login

This recipe uses the -d argument to add name=johnny%20depp to the body of a POST request to https://google.com/login. When using the -d argument, curl automatically adds the Content-Type: application/x-www-form-urlencoded header that tells the web server it's sending URL-encoded form data. Note that the key=value data should be URL-escaped.

URL-encode and Send Form Data

curl --data-urlencode 'name=john depp' https://google.com/login

This recipe uses the --data-urlencode argument to add name=john depp form data to a POST request. This argument works just like the -d argument in the previous recipe, except it also URL-encodes the data. Thus, the data this recipe adds to the body of the POST request is name=johnny%20depp and the request is then sent to https://google.com/login.

Using Multiple -d Arguments

curl -d 'foo=bar' -d 'baz=quux' https://google.com

The previous two recipes sent just a single variable in the POST request but you can send as many variables as you need. Just specify them one after another. This recipe POSTs two variables, foo=bar and baz=quux.

Send Multipart Data

curl -F 'name=johnny' https://google.com/search

This recipe uses the -F argument to send multipart form data in a POST request. When sending multipart data, curl sets the Content-Type header to multipart/form-data. Sending multipart data is useful when you need to upload images or other binary files.

Upload an Image

curl -F 'pic=@me.jpg' https://google.com/profile

This recipe also uses the -F argument but instead of sending textual data, it sends the file me.jpg. It uploads it to https://google.com/profile via a POST request. As this recipe uses the -F argument, this image is uploaded as multipart base64-encoded form data.

Upload an Image and Change Its Filename

curl -F 'pic=@me.jpg;filename=x.jpg' https://google.com/profile

This recipe adds a special argument filename=x.jpg after me.jpg. This tells curl to use a new filename for the attached file. Curl will read me.jpg but tell the server that the filename is x.jpg.

Send JSON Data

curl -H 'Content-Type: application/json' -d '{"query": "cats"}' https://google.com/search

This recipe sends a POST request with JSON data in it. It specifies the JSON data itself as the argument to the -d switch, and it also sets the request's content type to application/json via the -H argument so that the web server knew that the incoming data is JSON and not plain text.

Send XML Data

curl -H 'Content-Type: text/xml' -d 'hello' https://google.com/msg

This recipe sets the content type header to text/xml, which is the MIME type of XML. It then adds <message>hello</message> data to the request and POSTs it to https://google.com/msg. Sending XML data, JSON data, or any other text data is very similar. All you have to do is change the content type header to the correct MIME type so that the server and the application know what type of data it is.

Created by Browserling

These curl recipes were written down by me and my team at Browserling. We use recipes like this every day to get things done and improve our product. Browserling itself is an online cross-browser testing service powered by alien technology. Check it out!

Secret message: If you love my curl recipe, then I love you, too! Use coupon code CURLLING to get a discount at my company.