The curl
command is an extremely versatile and powerful tool for interacting with web servers and APIs. It is commonly used for tasks such as downloading files, testing API endpoints, automating web requests, and even performing complex data transfers. Mastering curl
is essential for developers, sysadmins, and anyone who regularly works with HTTP or other network protocols.
curl
命令是一个极其多功能且强大的工具,用于与 Web 服务器和 API 进行交互。它通常用于下载文件、测试 API 端点、自动化 Web 请求,甚至执行复杂的数据传输。掌握 curl
对于开发人员、系统管理员以及任何经常处理 HTTP 或其他网络协议的人来说都是必不可少的技能。
1. Basic Usage (基本用法)
[English] The simplest use of curl
is to fetch the contents of a URL. By default, curl
will send a GET request and output the content to the terminal.
Example:
curl http://example.com
What Happens: This command retrieves the contents of http://example.com
and displays it in the terminal.
Behind the Scenes: curl
sends an HTTP GET request to the specified URL, retrieves the response, and prints it to standard output.
基本用法
[中文] curl
最简单的用法是获取 URL 的内容。默认情况下,curl
会发送一个 GET 请求,并将内容输出到终端。
示例:
curl http://example.com
What Happens: 此命令检索 http://example.com
的内容,并将其显示在终端中。
Behind the Scenes: curl
向指定的 URL 发送一个 HTTP GET 请求,检索响应并将其打印到标准输出。
2. Downloading Files (下载文件)
[English] One of the most common uses of curl
is to download files from the internet. You can use the -O
option to save the file with the same name as in the URL, or -o
to specify a different filename.
Example:
curl -O http://example.com/file.txt
What Happens: This command downloads file.txt
from http://example.com
and saves it in the current directory with the same filename.
Behind the Scenes: curl
sends a GET request to the URL, retrieves the file, and writes it to disk using the filename provided by the server.
下载文件
[中文] curl
的一个最常见的用途是从互联网上下载文件。你可以使用 -O
选项保存文件,并使用与 URL 中相同的名称,或者使用 -o
来指定不同的文件名。
示例:
curl -O http://example.com/file.txt
What Happens: 此命令从 http://example.com
下载 file.txt
,并使用相同的文件名将其保存在当前目录中。
Behind the Scenes: curl
向 URL 发送 GET 请求,检索文件并使用服务器提供的文件名将其写入磁盘。
3. Sending Data with POST Requests (使用 POST 请求发送数据)
[English] curl
can send data to a server using the HTTP POST method, which is commonly used in forms and API requests. You can use the -d
option to send data as part of the request body.
Example:
curl -d "name=John&age=30" -X POST http://example.com/api
What Happens: This command sends the data name=John&age=30
to http://example.com/api
using an HTTP POST request.
Behind the Scenes: curl
constructs an HTTP POST request with the specified data in the request body and sends it to the server. The -X POST
option explicitly sets the request method to POST, although -d
implies POST.
使用 POST 请求发送数据
[中文] curl
可以使用 HTTP POST 方法将数据发送到服务器,这通常用于表单和 API 请求。你可以使用 -d
选项将数据作为请求体的一部分发送。
示例:
curl -d "name=John&age=30" -X POST http://example.com/api
What Happens: 此命令使用 HTTP POST 请求将数据 name=John&age=30
发送到 http://example.com/api
。
Behind the Scenes: curl
构建一个 HTTP POST 请求,将指定的数据放入请求体中并发送到服务器。-X POST
选项显式设置请求方法为 POST,尽管 -d
已经暗示 POST。
4. Adding Headers to Requests (向请求添加头部信息)
[English] When interacting with APIs or web servers, you often need to include custom headers in your requests. curl
allows you to add headers using the -H
option.
Example:
curl -H "Authorization: Bearer TOKEN123" http://example.com/api
What Happens: This command sends a GET request to http://example.com/api
with an Authorization
header containing Bearer TOKEN123
.
Behind the Scenes: curl
adds the specified header to the HTTP request, which is often necessary for authentication or to provide additional context to the server.
向请求添加头部信息
[中文] 在与 API 或 Web 服务器交互时,你通常需要在请求中包含自定义头部信息。curl
允许你使用 -H
选项添加头部信息。
示例:
curl -H "Authorization: Bearer TOKEN123" http://example.com/api
What Happens: 此命令向 http://example.com/api
发送一个带有 Authorization
头部信息的 GET 请求,其中包含 Bearer TOKEN123
。
Behind the Scenes: curl
将指定的头部信息添加到 HTTP 请求中,这通常是进行身份验证或向服务器提供附加上下文所必需的。
5. Saving Output to a File (将输出保存到文件)
[English] By default, curl
outputs the response to the terminal. However, you can redirect the output to a file using the -o
option.
Example:
curl -o output.txt http://example.com
What Happens: This command saves the response from http://example.com
to a file named output.txt
in the current directory.
Behind the Scenes: Instead of printing the response to the terminal, curl
writes it to the specified file, making it easy to save responses for later analysis.
将输出保存到文件
[中文] 默认情况下,curl
将响应输出到终端。但是,你可以使用 -o
选项将输出重定向到文件。
示例:
curl -o output.txt http://example.com
What Happens: 此命令将 http://example.com
的响应保存到当前目录中名为 output.txt
的文件中。
Behind the Scenes: curl
不会将响应打印到终端,而是将其写入指定的文件中,从而便于保存响应以供日后分析。
6. Handling Redirects (处理重定向)
[English] Many web servers use redirects to guide users to the correct URL. By default, curl
does not follow redirects, but you can enable this behavior using the -L
option.
Example:
curl -L http://example.com
What Happens: This command follows any redirects that the server sends, ensuring that you end up at the final destination URL.
Behind the Scenes: curl
will automatically handle HTTP 3xx redirect responses and follow the Location
header to retrieve the final content.
处理重定向
[中文] 许多 Web 服务器使用重定向来引导用户到正确的 URL。默认情况下,curl
不会跟随重定向,但你可以使用 -L
选项启用此行为。
示例:
curl -L http://example.com
What Happens: 此命令跟随服务器发送的任何重定向,确保你最终到达最终的目标 URL。
Behind the Scenes: curl
会自动处理 HTTP 3xx 重定向响应,并跟随 Location
头部信息以检索最终内容。
7. Verbose Mode for Debugging (调试的详细模式)
[English] When troubleshooting or debugging web requests, it can be helpful to see more details about what curl
is doing. The -v
option enables verbose mode, which provides detailed information about the request and response.
Example:
curl -v http://example.com
What Happens: This command displays detailed information about the connection, request headers, and response headers, along with the content of the response.
Behind the Scenes: The verbose output includes information about the connection process, HTTP request and response headers, and other details that are useful for debugging issues with web requests.
调试的详细模式
[中文] 在排查或调试 Web
请求时,查看 curl
的详细操作信息可能会有所帮助。-v
选项启用详细模式,提供有关请求和响应的详细信息。
示例:
curl -v http://example.com
What Happens: 此命令显示有关连接、请求头、响应头以及响应内容的详细信息。
Behind the Scenes: 详细输出包括有关连接过程、HTTP 请求和响应头以及其他对调试 Web 请求问题有用的详细信息。
Conclusion (总结)
[English] The curl
command is an incredibly powerful tool for interacting with web servers and APIs. Its versatility makes it invaluable for a wide range of tasks, from simple file downloads to complex API interactions. By mastering the different options and functionalities of curl
, you can automate and streamline your work with HTTP and other network protocols.
Chinese curl
命令是与 Web 服务器和 API 进行交互的一个极其强大的工具。它的多功能性使其在各种任务中都非常有价值,从简单的文件下载到复杂的 API 交互。通过掌握 curl
的不同选项和功能,你可以自动化并简化你在 HTTP 和其他网络协议上的工作。
Linux/Mac Terminal Tutorial: How To Use The cURL Command by Corey Schafer
Leave a Reply