curl command in Linux

curl is a tool for transferring data from or to a server. It supports these protocols: DICT, FILE, FTP, FTPS, GOPHER, gopers, Http, https, imap, imaps, ldap, ldaps, mqtt, pop3, pop3s, rtmp, rtmp, rtsp, scp, sftp, smb, smbs, smtp , smtps, telnet, tftp, ws WSS. This command is designed to work without user interaction.
Options:
–abstract-unix-socket

(HTTP) Connect via an abstract Unix domain socket instead of using the network

-a, –append

When uploading files using ftp, append content to the target file instead of overwriting it. If the remote file does not exist, it will be created.

-K, –config

Specify a file for curl to read as an option parameter

–connect-timeout

Specify the curl connection timeout, the unit is seconds, you can specify a decimal

Example:

[root@192 ~]# curl --connect-timeout 20 www.baidu.com

-C, –continue-at

Resume the terminated transfer process. When downloading a file, an unexpected termination request is encountered. Add this option to continue downloading. Offset can specify where to offset the download. The unit is k. If offset does not give a value, then You can use -C - let curl automatically infer the offset position

-c, –cookie-jar

Save the cookie in memory after the request is completed

-b, –cookie

When requesting, pass the cookie to the http service header

–create-dirs

When saving the downloaded file, you can specify the directory to save. If the directory does not exist, it will be created automatically. The default owner permission of the file is 0750.

–create-file-mode

When using ftp to upload files, bring this option to specify the owner permissions. The default is 0644.

–data-binary

Transfer binary data to the server, starting with @ means it is a file

–data-raw

Transmit data as is, without parsing or escaping

–data-urlencode

The requested data is url encoded

-d, –data

Send a post request to the server and specify the request parameters

Example:

curl -d "name=curl" https://example.com

curl -d "name=curl" -d "tool=cmdline" https://example.com

This can be combined as: curl -d "name=curl & amp;tool=cmdline" https://example.com

curl -d @filename https://example.com

-D, –dump-header

Write request header information to the specified file

-F, –form

Send http form request data

-G, –get

Use the get method to send an http request. If -d, --data, --data-binary or --data-urlencode is used in the command, all request parameters are placed after the url as the query string.

Example:

curl --get https://example.com

curl --get -d "tool=curl" -d "age=old" https://example.com

-g, –globoff

Close the global parser. Symbols that need to be parsed and escaped will be treated as ordinary strings.

Example:

curl -g "https://example.com/{[]}}}}"

-I, –head

Only output request headers

Example:

curl -I https://example.com

-H, –header header/@file

Specify the request header. You can set the request header to empty to override the default request header, for example: -H "Host:"

Example:

curl -H "X-First-Name: Joe" https://example.com

curl -H "User-Agent: yes-please/2000" https://example.com

curl -H "Host:" https://example.com

curl -H @headers.txt https://example.com

–json

Specify http json request and send json data
This option is a combination of the following three options:

--data [arg]
--header "Content-Type: application/json"
--header "Accept: application/json"

Example:

curl --json '{ "drink": "coffe" }' https://example.com
curl --json @prepared https://example.com
curl --json @- https://example.com < json.txt

–keepalive-time

The time interval for sending heartbeats to keep the connection, unit: seconds

Example:

curl --keepalive-time 20 https://example.com

–libcurl

Specify a file name, and then the curl request process will write the file in the form of C source code.

Example:

curl --libcurl client.c https://example.com

–limit-rate

Limit the transmission rate. The default unit is K. K, M, G, T, and P are available.

Example:

curl --limit-rate 100K https://example.com

curl --limit-rate 1000 https://example.com

curl --limit-rate 10M https://example.com

-l, –list-only

List ftp directories

Example:

curl --list-only ftp://example.com/dir/

-L, –location

If the page requested by curl is redirected, it will automatically follow the request.

Example:

curl -L https://example.com

–max-filesize

Maximum file download size, available units: K, M, G

Example:

curl --max-filesize 100K https://example.com

–max-redirs

Maximum number of redirects, used in combination with -L

Example:

curl -L --max-redirs 3 https://example.com

-:, –next

You can specify multiple requests, and each request can use a different request method. For example, first use post request, and then use get request. This option is to reset the current option.

Example:

curl www1.example.com --next -d postthis www2.example.com

-o, –output

Specify the saved file name when downloading the file

Example:

General usage
curl -o aa example.com

#1 corresponds to the variable {one, two}, which is automatically named.
curl "http://{one,two}.example.com" -o "file_#1.txt"

#1, #2 correspond to {site,host} [1-5] respectively
curl "http://{site,host}.host[1-5].com" -o "#1_#2"

Suppress output, redirect to trash
curl example.com -o /dev/null

-#, –progress-bar

Display a progress bar with a # symbol instead of verbose output

Example:

curl -# -O https://example.com

-x, –proxy [protocol://]host[:port]

Use a proxy server to request. The default is to use the http protocol. When the port is not specified, the default port is 1080.

Example:

curl -x 127.0.0.1:9090 https://example.com

-U, –proxy-user user:password

When using a proxy server, you need to specify a username and password

Example:

curl --proxy-user name:pwd -x proxy https://example.com

–rate

Limit the rate of retry requests, the format is: N/U, N represents the number of requests, U represents the unit, the available units are: s (second), m (minute), h (hour), d (day ), the default unit is per hour, the internal function of this option uses a millisecond level scheme, if the setting exceeds 1000 times per second, the option will become unlimited rate

Example:

curl --rate 2/s https://example.com

curl --rate 3/h https://example.com

curl --rate 14/m https://example.com

-e, –referer

Specify the source address. If specified as: ";auto", curl will automatically infer

Example:

curl --referer "https://fake.example" https://example.com

curl --referer "https://fake.example;auto" -L https://example.com

curl --referer ";auto" -L https://example.com

-O, –remote-name

When downloading a file, specifying this option will use the name of the remote file

Example:

curl -O https://example.com/filename

–retry

Specify the number of curl retries

Example:

curl --retry 7 https://example.com

-s, –silent

Silent mode, no information is output

Example:

curl -s https://example.com

–trace

Print complete input, output, error and other information, and you can specify the output file

Example:

curl --trace log.txt https://example.com

-T, –upload-file

Tell curl that the file is currently being uploaded via ftp

Example:

curl -T file https://example.com

curl -T "img[1-1000].png" ftp://ftp.example.com/

-A, –user-agent

Specify the user agent of the http request header

Example:

curl -A "Agent 007" https://example.com

-v, –verbose

Print detailed debug information

Example:

curl -v https://example.com

-V, –version

Print curl version information

-w, –write-out

Use curl predefined variables to print out useful information about the input and output operation process.

Example:

curl -w '%{response_code}\
' https://example.com