cURL

cURL which stands for "client for URL" is a useful networking tool that can be used in command lines or scripts for transferring data. It supports multiple protocols.

Basic commands:

# Basic
$ curl -X <method> <HTTP_URL>

# With headers
$ curl -X <method> -H <headers> <HTTP_URL>

# GET: no need to specify the -X flag
$ curl -H <headers> <HTTP_URL>

# VERBOSE: increasing level of output
$ curl ... -v
$ curl ... -vv
$ curl ... -vvv

Examples

Sending a GET request to https://domain.comwith query data

$ curl https://domain.com?key=value

POST

x-www-form-urlencoded

# no need to specify -X POST
$ curl -H "content-type:application/x-www-form-urlencoded" -d/--data <POST_data> <HTTP_URL> 

Eg. Sending a POST request to https://domain.com with some x-www-form-urlencoded data.

$ curl https://domain.com -H ... -d "key1=value1&key2=value2"
# OR
$ curl https://domain.com -H ... --data "key1=value1&key2=value2"

json

# no need to specify -X POST
$ curl -H 'application/json' -d/--data <POST_data> <HTTP_URL> 

Eg. Sending a POST request to https://domain.com with some json data.

$ curl https://domain.com -H ... -d '{"key1":"value1","key2":"value2"}'
# OR
$ curl https://domain.com -H ... --data '{"key1":"value1","key2":"value2"}'

Form-data

The @ and < symbols can be used to read contents from a file for file upload and text field respectively.

From the cURL documentation:

To force the 'content' part to be a file, prefix the file name with an @ sign. To just get the content part from a file, prefix the file name with the symbol <. The difference between @ and < is then that @ makes a file get attached in the post as a file upload, while the < makes a text field and just get the contents for that text field from a file.

# no need to specify -X POST
$ curl -H <headers> -F/--form <form_data> <HTTP_URL>

# Eg.
$ curl -H <headers> -F "fileToUpload=@example.php;type=text/x-php" -F "submit=Upload" 

SOCKS5 proxy

$ curl --socks5 [addr]:[port] [url]
$ curl -x socks5://[addr]:[port] [url]

# eg. Connect to https://example.com while proxying traffic through 
# SOCKS5 proxy at 10.10.10.10 port 8888
$ curl --socks5 10.10.10.10:8888 https://example.com
$ curl -x socks5://10.10.10.10:8888 https://example.com

To make the proxy handle DNS resolutions, for privacy concerns, etc. This means that there will be no local address resolution.

The --socks5-hostname or -x socks5h:// options can be used instead.

$ curl --socks5-hostname [addr]:[port] [url]
$ curl -x socks5h://[addr]:[port] [url]

curl documentation

Last updated