链接:https://www.cnblogs.com/huangSir-devops/p/18875948
Detailed Usage of the curl Command in Linux Systems
The table of contents is here, please message me for any modifications
- • Overview of curl
- • Application Scenarios of curl
- • Basic Usage of curl
- • Installation
- • Syntax of curl
- • Common Options and Parameters
- • Common Use Cases
Overview of curl
Official Documentation: https://curl.se/docs/GitHub Repository: https://github.com/curl/curl
curl, short for Client URL, is a command-line tool and library for transferring data via URLs, supporting over 100 protocols (HTTP/HTTPS, FTP, SMTP, POP3, RTSP, DICT, etc.).
Its main functions include sending network requests, receiving responses, supporting file uploads/downloads, handling authentication, proxies, cookies, redirection, and other complex network operations.
Its lightweight, simplicity, cross-platform compatibility (Linux, Windows, Mac), and script-friendly nature (suitable for automation and API testing) make it popular among programmers.
Application Scenarios of curl
- • API Debugging: Testing RESTful interfaces (GET/POST/PUT/DELETE, etc.).
- • File Transfer: Downloading remote files, uploading files to servers (e.g., FTP/SCP).
- • Network Diagnostics: Checking server response status, analyzing request/response headers.
- • Data Collection: Scraping web content (be mindful of the website’s robots.txt protocol).
- • Script Automation: Integrating network requests in Shell/Python scripts.
Basic Usage of curl
Installation
| | |
| --- | --- |
| | # For Debian-based Linux |
| | apt install -y curl |
| | |
| | # For CentOS-based Linux |
| | yum install -y curl |
Syntax of curl
| | |
| --- | --- |
| | # where options are the option parameters, and url is the request path |
| | curl [options] [url] |
Common Options and Parameters
Request Control Options
- • -X, –request: Specify the request method (GET, POST, PUT, DELETE…)
| | |
| --- | --- |
| | curl -X GET https://www.baidu.com |
- • -H, –header: Add request headers, can be specified multiple times
| | |
| --- | --- |
| | curl -H "content-type: application/json; charset=utf-8" \
| | -H "Authorization: Bearer TOKEN" \
| | https://api.example.com |
- • -d: Send POST data (form data, automatically sets Content-Type: application/x-www-form-urlencoded)
| | |
| --- | --- |
| | curl -d "name=John&age=30" https://api.example.com/submit |
- • -D, –dump-header: Save response headers to a file
| | |
| --- | --- |
| | curl -D headers.txt https://www.baidu.com |
- • -L, –location: Automatically follow redirects (301/302 status codes)
| | |
| --- | --- |
| | curl -L http://www.baidu.com |
- • -b, –cookie: Send cookies (format: name=value or file path)
| | |
| --- | --- |
| | curl -b "sessionid=12345" https://example.com |
- • -c, –cookie-jar: Save cookies returned by the server to a local file for later access
| | |
| --- | --- |
| | curl -c cookies.txt https://example.com/login |
Data Transfer Options
- • -o: Save downloaded content to a file
| | |
| --- | --- |
| | curl -o download.zip https://example.com/file.zip |
- • -O: Download content, file name follows the remote file
| | |
| --- | --- |
| | curl -O https://example.com/file.zip |
- • -d: Send POST data (form data, automatically sets Content-Type: application/x-www-form-urlencoded)
| | |
| --- | --- |
| | curl -d "name=John&age=30" https://api.example.com/submit |
- • -F, –form: Send form files/data (simulating browser upload, automatically sets multipart/form-data)
| | |
| --- | --- |
| | curl -F "[email protected]" https://api.example.com/upload |
- • -T, –upload-file: Upload a file (PUT or POST request)
| | |
| --- | --- |
| | curl -T report.pdf ftp://ftp.example.com/upload/ |
Output and Debugging Options
- • -v, –verbose: Show detailed debugging information (request headers, response headers, connection process, etc.)
| | |
| --- | --- |
| | curl -v https://www.baidu.com |
- • -s, –silent: Silent mode (does not show progress bar), but error messages will still be displayed.
| | |
| --- | --- |
| | curl -s https://example.com > content.txt |
- • -S, –show-error: Show error messages in silent mode (must be used with -s)
| | |
| --- | --- |
| | curl -sS https://invalid-url.com |
- • –trace: Trace the request process (more detailed debugging logs)
| | |
| --- | --- |
| | curl --trace trace.log https://example.com |
Other Advanced Options
- • –proxy: Use a proxy server (format: host:port, supports HTTP/SOCKS5)
| | |
| --- | --- |
| | curl --proxy 127.0.0.1:8080 https://example.com |
- • –insecure, -k: Ignore HTTPS certificate verification (unsafe, for testing only)
| | |
| --- | --- |
| | curl -k https://self-signed.example.com |
- • –user, -u: Basic authentication (username:password)
| | |
| --- | --- |
| | curl -u admin:password123 https://auth.example.com |
- • –ntlm: Use NTLM authentication (Windows domain environment)
| | |
| --- | --- |
| | curl --ntlm -u domain\user:password https://intranet.example.com |
- • –limit-rate: Limit transfer speed (e.g., 100k means 100KB/s)
| | |
| --- | --- |
| | curl --limit-rate 100k https://example.com/big-file.zip |
- • –retry: Number of retries after failure (useful during network fluctuations)
| | |
| --- | --- |
| | curl --retry 5 https://flaky-api.example.com |
- • –range: Resume downloads (request partial content, useful for downloading large files)
| | |
| --- | --- |
| | curl --range 0-1024 https://example.com/large-file.zip |
- • -x, –compressed: Enable compression (automatically handles gzip/bzip2 encoding)
| | |
| --- | --- |
| | curl -x https://example.com |
Common Use Cases
- • Simulating Domain Access
| | |
| --- | --- |
| | # Purpose: Request 192.0.2.1, but tell the server "I am fake.example.com" |
| | curl -H "Host: fake.example.com" http://192.0.2.1 |
- • Downloading Files
| | |
| --- | --- |
| | # Short option -o, specify output file name |
| | curl -o filename.html https://example.com/page.html |
| | |
| | # Uppercase -O, use the file name from the URL |
| | curl -O https://example.com/filename.zip |
(Copyright belongs to the original author, please delete if infringed)
Disclaimer: The content of this article is sourced from the internet and is for reference only. Reproduction is for learning and communication purposes. If your legitimate rights are inadvertently infringed, please contact the Docker Chinese community promptly!