curl是一个很棒的工具,可让您从命令行创建网络请求
curl是一个命令行工具,可用于在网络上传输数据。
它支持许多现成的协议,包括HTTP,HTTPS,FTP,FTPS,SFTP,IMAP,SMTP,POP3等。
当涉及调试网络请求时,curl是您可以找到的最好的工具之一。
它是您一旦知道如何使用的工具之一,便会不断得到使用。程序员最好的朋友。
它具有通用性,可以在Linux,Mac,Windows上运行。请参阅官方安装指南将其安装在您的系统上。
有趣的事实:curl的作者和维护者,瑞典人,由于他的工作(curl和libcurl)对计算世界的贡献而被瑞典国王授予。
让我们深入研究一些在处理HTTP请求时最可能要执行的命令和操作。
这些示例涉及使用最流行的协议HTTP。
- 执行HTTP GET请求
- 获取HTTP响应头
- 仅获取HTTP响应标头
- 执行HTTP POST请求
- 执行HTTP POST请求以发送JSON
- 执行HTTP PUT请求
- 跟随重定向
- 将响应存储到文件
- 使用HTTP身份验证
- 设置其他用户代理
- 检查请求和响应的所有详细信息
- 将任何浏览器网络请求复制到curl命令
执行HTTP GET请求
当您执行请求时,curl将返回响应的主体:
curl https://flaviocopes.com/
获取HTTP响应头
默认情况下,响应标头隐藏在curl的输出中。要显示它们,请使用i
选项:
curl -i https://flaviocopes.com/
仅获取HTTP响应标头
使用I
选项,你可以得到只要标头,而不是响应正文:
curl -I https://flaviocopes.com/
执行HTTP POST请求
这X
选项可让您更改所使用的HTTP方法。默认情况下,使用GET,它与编写相同
curl -X GET https://flaviocopes.com/
使用-X POST
将执行POST请求。
您可以执行POST请求,传递经过编码的数据URL:
curl -d "option=value&something=anothervalue" -X POST https://flaviocopes.com/
在这种情况下,application/x-www-form-urlencoded
Content-Type已发送。
执行HTTP POST请求以发送JSON
您可能希望发送JSON,而不是像上面的示例那样发布经过URL编码的数据。
在这种情况下,您需要使用H
选项:
curl -d '{"option": "value", "something": "anothervalue"}' -H "Content-Type: application/json" -X POST https://flaviocopes.com/
您还可以从磁盘发送JSON文件:
curl -d "@my-file.json" -X POST https://flaviocopes.com/
执行HTTP PUT请求
该概念与POST请求的概念相同,只需使用以下命令更改HTTP方法-X PUT
跟随重定向
像301这样的重定向响应,它指定了Location
响应标头,然后可以通过指定L
选项:
curl http://flaviocopes.com/
不会自动遵循我设置为重定向到的HTTPS版本,但这将:
curl -L http://flaviocopes.com/
将响应存储到文件
使用o
选项,您可以告诉curl将响应保存到文件:
curl -o file.html https://flaviocopes.com/
您还可以使用文件名按文件名将其保存在服务器上O
选项:
curl -O https://flaviocopes.com/index.html
使用HTTP身份验证
如果资源需要基本HTTP身份验证,则可以使用u
传递user:password值的选项:
curl -u user:pass https://flaviocopes.com/
设置其他用户代理
用户代理告诉服务器哪个客户端正在执行请求。默认情况下,curl发送curl/<version>
用户代理,例如:curl/7.54.0
。
您可以使用--user-agent
选项:
curl --user-agent "my-user-agent" https://flaviocopes.com
检查请求和响应的所有详细信息
使用--verbose
使curl输出请求的所有详细信息以及响应的选项:
curl --verbose -I https://flaviocopes.com/
* Trying 178.128.202.129...
* TCP_NODELAY set
* Connected to flaviocopes.com (178.128.202.129) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate: flaviocopes.com
* Server certificate: Let's Encrypt Authority X3
* Server certificate: DST Root CA X3
> HEAD / HTTP/1.1
> Host: flaviocopes.com
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Cache-Control: public, max-age=0, must-revalidate
Cache-Control: public, max-age=0, must-revalidate
< Content-Type: text/html; charset=UTF-8
Content-Type: text/html; charset=UTF-8
< Date: Mon, 30 Jul 2018 08:08:41 GMT
Date: Mon, 30 Jul 2018 08:08:41 GMT
...Copying any browser network request to a curl command
When inspecting any network request using the Chrome Developer Tools, you have the option to copy that request to a curl request:

curl 'https://github.com/curl/curl' -H 'Connection: keep-alive' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache' -H 'Upgrade-Insecure-Requests: 1' -H 'DNT: 1' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'Referer: https://www.google.it/' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.9,it;q=0.8' -H 'Cookie: _octo=GH1.1.933116459.1507545550; _ga=GA1.2.643383860.1507545550; tz=Europe%2FRome; user_session=XXXXX; __Host-user_session_same_site=YYYYYY; dotcom_user=flaviocopes; logged_in=yes; has_recent_activity=1; _gh_sess=ZZZZZZ' --compressed
More network tutorials:
- Introduction to WebSockets
- How HTTP requests work
- The HTTP Request Headers List
- The HTTP Response Headers List
- HTTP vs HTTPS
- What is an RFC?
- The HTTP protocol
- The HTTPS protocol
- The curl guide to HTTP requests
- Caching in HTTP
- The HTTP Status Codes List
- What is a CDN?
- The HTTP/2 protocol
- What is a port
- DNS, Domain Name System
- The TCP Protocol
- The UDP Protocol
- An introduction to REST APIs
- How to install a local SSL certificate in macOS
- How to generate a local SSL certificate
- How to configure Nginx for HTTPS
- A simple nginx reverse proxy for serving multiple Node.js apps from subfolders
- What is a reverse proxy?