Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux curl command

Linux curl command

Linux Command Reference Linux Command Reference


What is the curl command

curl (Client URL) is a powerful command-line tool for transferring data on Linux/Unix systems. It supports many protocols, including HTTP, HTTPS, FTP, SFTP, and more, making it indispensable in the daily work of developers and system administrators.

Key features of curl

  1. Multi-protocol support: Supports almost all mainstream network protocols
  2. No graphical interface: A pure command-line tool, ideal for scripts and automation tasks
  3. Feature-rich: Supports file upload and download, form submission, cookie handling, and more
  4. Cross-platform: Available on Linux, macOS, and Windows

Basic syntax structure

The basic syntax of the curl command is as follows:



curl [options] [URL...]

Parameter Description


Detailed explanation of common options

1. Basic request control

Option Description Example
-X Specify the HTTP method curl -X POST https://example.com
-d Send POST data curl -d "name=John" https://example.com
-G Send -d data as GET parameters curl -G -d "q=keyword" https://search.com
-H Add request headers curl -H "Content-Type: application/json" https://api.com

2. Output control

Option Description Example
-o Save output to a file curl -o output.html https://example.com
-O Save using the remote file name curl -O https://example.com/file.zip
-s Silent mode (no progress display) curl -s https://api.com/data.json
-v Display detailed communication process curl -v https://example.com

3. Authentication and security

Option Description Example
-u Username and password authentication curl -u user:pass https://secure.com
-k Ignore SSL certificate verification curl -k https://self-signed.com
--cacert Specify a CA certificate curl --cacert cert.pem https://secure.com

4. Other useful options

Option Description Example
-L Follow redirects curl -L https://short.url
-I Retrieve header information only curl -I https://example.com
--limit-rate Limit transfer speed curl --limit-rate 100K https://largefile.com

Practical example analysis

Example 1: Fetch web page content

Example

curl https:

//

www.example.com

This simplest command fetches the content of the specified URL and displays it in the terminal.

Example 2: Download a file

Example

curl

-O

https:

//

example.com

/

files

/

document.pdf

-O The option saves the file locally using the remote file name (document.pdf).

Example 3: Send a POST request

Example

curl

-X

POST

-d

"username=admin&password=123456"

https:

//

api.example.com

/

login

This command sends a username and password to a login endpoint.

Example 4: POST request with JSON data

Example

curl

-X

POST

-H

"Content-Type: application/json"

\

-d

'{"name":"John","age":30}'

\

https:

//

api.example.com

/

users

Example 5: Maintain a session using cookies

Example

curl

-c

cookies.txt

-b

cookies.txt https:

//

member.example.com


Advanced usage

1. File upload

Example

curl

-F

"file=@localfile.txt"

https:

//

upload.example.com

-F Used for multipart/form-data form submissions, commonly for file uploads.

2. Test website response time

Example

curl

-o

/

dev

/

null

-s

-w

"Connect: %{time_connect} TTFB: %{time_starttransfer} Total: %{time_total}\n"

https:

//

example.com

This command outputs:

3. Using a proxy server

Example

curl

-x

http:

//

proxy.example.com:

8080

https:

//

target.com

-x The option specifies the proxy server address and port.


Frequently Asked Questions

Q1: How to resolve curl certificate verification errors?

When encountering an SSL certificate error, you can temporarily use -k the option to ignore verification (not recommended for production), or properly configure the CA certificate:

Example

curl

--cacert

/

path

/

to

/

cert.pem https:

//

secure-site.com

Q2: How to display request and response headers?

Use -v or -i Option:

Example

curl

-v

https:

//

example.com  

Display detailed communication process

curl

-i

https:

//

example.com  

Display response headers only

Q3: How to limit the download speed?

Use --limit-rate option (units can be K, M):

Example

curl

--limit-rate

200K

-O

https:

//

example.com

/

largefile.iso


Practical Exercise

  1. Try downloading data from a public JSON API and saving it to a file
  2. Use curl to test the response time of your local development server
  3. Practice sending a GET request with an authentication header
  4. Try uploading a test file to a free HTTP bin service

Example

Practice examples

curl https:

//

httpbin.org

/

get

curl

-o

test.json https:

//

api.github.com

/

users

/

octocat

curl

-X

POST

-F

"file=@test.txt"

https:

//

httpbin.org

/

post


Summary

curl is a powerful network data transfer tool on Linux. Through this article, you should have mastered:

  1. curl's basic syntax and common options
  2. Usage examples in various common scenarios
  3. Advanced features and practical tips
  4. Solutions to common problems

To learn more details, you can consult curl's official documentation or use man curl to view the man page. As you gain practical experience, you will find that curl can do far more than these basic functions.

Linux Command Reference Linux Command Reference