zhcn 技术 手机 非公開: 10个cURL命令用法及实时示例

10个cURL命令用法及实时示例

cURL 是一个命令行工具,用于使用 URL 语法检索或发送数据。

如果您是开发人员或在支持部门工作,则需要了解如何使用 cURL 命令来排除 Web 应用程序故障。 cURL 是一个跨平台实用程序,适用于 Windows、MAC 和 UNIX。

以下是一些最常用的语法和有用的示例。

10个cURL命令用法及实时示例
10个cURL命令用法及实时示例

检查是否可以连接到该 URL

如果您在 UNIX 系统上工作并尝试连接到外部 URL,首先要做的是检查该 URL 是否可以通过curl 访问。

curl yoururl.com

没有输出被抛出。但是,如果无法联系服务器,您将收到诸如主机无法解析之类的错误。

 [root@gf-lab tmp]# curl helloitdoesntexist.com
curl: (6) Could not resolve host: helloitdoesntexist.com; Unknown error
[root@gf-lab tmp]# 
10个cURL命令用法及实时示例
10个cURL命令用法及实时示例

将 URL/URI 输出保存到文件

如果需要将 URL 或 URI 的内容保存到特定文件,可以使用以下语法:

 curl https://yoururl.com > yoururl.html

原来的:

 [root@gf-lab tmp]# curl https://gf.dev > /tmp/gfhtml
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 18557    0 18557    0     0  72565      0 --:--:-- --:--:-- --:--:-- 72772
[root@gf-lab tmp]#

在上面的例子中,gf.dev的所有内容将被保存到/tmp/gf.html。

10个cURL命令用法及实时示例
10个cURL命令用法及实时示例

查看请求和响应标头

如果您遇到问题并想要验证,您将按预期获得请求和响应标头。

 curl -v yoururl.com

原来的:

 [root@gf-lab tmp]# curl -v https://.com
* About to connect() to .com port 443 (#0)
*   Trying 104.25.134.107...
* Connected to .com (104.25.134.107) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
* Server certificate:
* 	subject: CN=ssl371609.cloudflaressl.com,OU=PositiveSSL Multi-Domain,OU=Domain Control Validated
* 	start date: Nov 07 00:00:00 2019 GMT
* 	expire date: May 15 23:59:59 2020 GMT
* 	common name: ssl371609.cloudflaressl.com
* 	issuer: CN=COMODO ECC Domain Validation Secure Server CA 2,O=COMODO CA Limited,L=Salford,ST=Greater Manchester,C=GB
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: .com
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Sat, 09 Nov 2019 19:41:37 GMT
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Set-Cookie: __cfduid=d2ce6cd359ebc0b6eb5ff3a454ed042021573328497; expires=Sun, 08-Nov-20 19:41:37 GMT; path=/; domain=..com; HttpOnly; Secure
< Vary: Accept-Encoding
< Link: <https://.com/wp-json/>; rel="https://api.w.org/"
< Link: <https://.com/>; rel=shortlink
< X-SRCache-Fetch-Status: HIT
< X-SRCache-Store-Status: BYPASS
< X-Frame-Options: SAMEORIGIN
< X-Powered-By: EasyEngine v4.0.12
< Via: 1.1 google
< CF-Cache-Status: DYNAMIC
< Strict-Transport-Security: max-age=15552000; preload
< X-Content-Type-Options: nosniff
< Alt-Svc: h3-23=":443"; ma=86400
< Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
< Server: cloudflare
< CF-RAY: 533243e4bcd4bbf4-LHR
< 
10个cURL命令用法及实时示例
10个cURL命令用法及实时示例

限速下载

如果您正在进行优化并想查看以特定速度下载需要多长时间,您可以:

 curl –-limit-rate 2000B

原来的:

 curl –-limit-rate 2000B https://gf.dev

使用代理连接

如果您使用的 DMZ 服务器需要使用代理连接到外部世界,则非常有用。

 curl --proxy yourproxy:port https://yoururl.com

插入标头和测试 URL

通过将标头插入数据中,您可以使用curl 来测试或解决特定问题。让我们看一下下面使用 Content-Type 发出请求的示例。

 curl --header 'Content-Type: application/json' http://yoururl.com

运行上面的命令将要求curl在请求标头中将Content-Type作为application/json传递。

仅显示响应标头

如果您正在排除故障并希望快速查看响应标头,可以使用以下语法:

 curl --head http://yoururl.com

原来的:

 [root@gf-lab tmp]# curl --head https://chandan.io
HTTP/1.1 200 OK
Date: Sat, 09 Nov 2019 19:51:23 GMT
Content-Type: text/html
Connection: keep-alive
Set-Cookie: __cfduid=d3cb2c7b8e566ad99c870b0af12b0f1eb1573329083; expires=Sun, 08-Nov-20 19:51:23 GMT; path=/; domain=.chandan.io; HttpOnly
X-GUploader-UploadID: AEnB2Uo96JhvJmR2zYUL-Ndh2ta3UD_ykQAB5C7O8cjZQhCf-GxHQ0MsodSzRnl3guSN3ywAYNjtWcPXfwDXjLg3bQ-P5vQMOA
Expires: Sat, 09 Nov 2019 20:51:23 GMT
Cache-Control: public, max-age=3600
Last-Modified: Mon, 06 Aug 2018 10:45:47 GMT
x-goog-generation: 1533552347482034
x-goog-metageneration: 1
x-goog-stored-content-encoding: identity
x-goog-stored-content-length: 24620
x-goog-hash: crc32c=DpDPAQ==
x-goog-hash: md5=cIP/3rusdUx12Zla1kf1yA==
x-goog-storage-class: MULTI_REGIONAL
Accept-Ranges: bytes
CF-Cache-Status: DYNAMIC
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
CF-RAY: 53325234dc2fbb9a-LHR

[root@gf-lab tmp]# 

连接到 HTTPS/SSL URL 并忽略 SSL 证书错误。

当您尝试访问受 SSL/TLS 证书保护的 URL 时,如果证书不正确或 CN 不匹配,您将收到以下错误:

 curl: (51) Unable to communicate securely with peer: requested domain name does not match the server's certificate.

好消息。您可以使用--insecure标志告诉 cURL 忽略证书错误。

 curl --insecure https://yoururl.com

使用特定协议 (SSL/TLS) 连接

对于测试特定 URL 是否可以通过特定 SSL/TLS 协议进行握手非常有用。

使用 SSL v3 进行连接

curl --sslv3 https://yoururl.com

对于不同的 TLS 版本

curl --tlsv1 https://example.com
curl --tlsv1.0 https://example.com
curl --tlsv1.1 https://example.com
curl --tlsv1.2 https://example.com
curl --tlsv1.3 https://example.com

从 FTP 服务器下载文件

您还可以通过指定用户名和密码使用curl来下载文件。

 curl -u user:password -O ftp://ftpurl/style.css

您始终可以将“ -v ”与任何语法一起使用以在详细模式下打印。

使用主机标头

当仅当主机标头匹配时请求的内容才可用时,主机标头对于通过 IP 测试目标 URL 非常有用。或者,如果您想使用负载均衡器 IP/URL 测试您的应用程序。

 curl --header 'Host: targetapplication.com' https://192.0.0.1:8080/

想尝试在线使用 cURL 吗?

是的,可以使用以下工具。您可以远程运行 cURL。

Online CURL – 在线检索 URL 的轻量级工具,具有附加选项:

 --connect-timeout
--cookie
--data
--header
--head
--location
--max-time
--proxy
--request
--user
--url
--user-agent
   

cURL 命令行生成器– 这是不同的。这可以帮助您构建curl命令,使您可以在漂亮的UI中输入信息,并在底部显示cURL命令。

卷曲生成器
卷曲生成器

cURL 是一个帮助解决实时连接问题的实用程序。我希望以上信息对您有所帮助。如果您想了解更多信息,我们推荐Linux 命令行基础知识在线课程。

浅显易懂的《10个cURL命令用法及实时实例》讲解!您必须观看的 2 个最佳视频

【Linuxコマンド】curlコマンドの実例
https://www.youtube.com/shorts/8zaELXNO2c0
curlコマンドで天気予報を見てみた
https://www.youtube.com/watch?v=i7imSVhdQnw&pp=ygVCIDEwIGNVUkwg44Kz44Oe44Oz44OJ44Gu5L2_55So5rOV44Go44Oq44Ki44Or44K_44Kk44Og44Gu5L6LJmhsPUpB

cURL 是一个命令行工具,用于使用 URL 语法检索或发送数据。

如果您是开发人员或在支持部门工作,则需要了解如何使用 cURL 命令来排除 Web 应用程序故障。 cURL 是一个跨平台实用程序,适用于 Windows、MAC 和 UNIX。

以下是一些最常用的语法和有用的示例。

10个cURL命令用法及实时示例
10个cURL命令用法及实时示例

检查是否可以连接到该 URL

如果您在 UNIX 系统上工作并尝试连接到外部 URL,首先要做的是检查该 URL 是否可以通过curl 访问。

curl yoururl.com

没有输出被抛出。但是,如果无法联系服务器,您将收到诸如主机无法解析之类的错误。

 [root@gf-lab tmp]# curl helloitdoesntexist.com
curl: (6) Could not resolve host: helloitdoesntexist.com; Unknown error
[root@gf-lab tmp]# 
10个cURL命令用法及实时示例
10个cURL命令用法及实时示例

将 URL/URI 输出保存到文件

如果需要将 URL 或 URI 的内容保存到特定文件,可以使用以下语法:

 curl https://yoururl.com > yoururl.html

原来的:

 [root@gf-lab tmp]# curl https://gf.dev > /tmp/gfhtml
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 18557    0 18557    0     0  72565      0 --:--:-- --:--:-- --:--:-- 72772
[root@gf-lab tmp]#

在上面的例子中,gf.dev的所有内容将被保存到/tmp/gf.html。

10个cURL命令用法及实时示例
10个cURL命令用法及实时示例

查看请求和响应标头

如果您遇到问题并想要验证,您将按预期获得请求和响应标头。

 curl -v yoururl.com

原来的:

 [root@gf-lab tmp]# curl -v https://.com
* About to connect() to .com port 443 (#0)
*   Trying 104.25.134.107...
* Connected to .com (104.25.134.107) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
* Server certificate:
* 	subject: CN=ssl371609.cloudflaressl.com,OU=PositiveSSL Multi-Domain,OU=Domain Control Validated
* 	start date: Nov 07 00:00:00 2019 GMT
* 	expire date: May 15 23:59:59 2020 GMT
* 	common name: ssl371609.cloudflaressl.com
* 	issuer: CN=COMODO ECC Domain Validation Secure Server CA 2,O=COMODO CA Limited,L=Salford,ST=Greater Manchester,C=GB
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: .com
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Sat, 09 Nov 2019 19:41:37 GMT
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Set-Cookie: __cfduid=d2ce6cd359ebc0b6eb5ff3a454ed042021573328497; expires=Sun, 08-Nov-20 19:41:37 GMT; path=/; domain=..com; HttpOnly; Secure
< Vary: Accept-Encoding
< Link: <https://.com/wp-json/>; rel="https://api.w.org/"
< Link: <https://.com/>; rel=shortlink
< X-SRCache-Fetch-Status: HIT
< X-SRCache-Store-Status: BYPASS
< X-Frame-Options: SAMEORIGIN
< X-Powered-By: EasyEngine v4.0.12
< Via: 1.1 google
< CF-Cache-Status: DYNAMIC
< Strict-Transport-Security: max-age=15552000; preload
< X-Content-Type-Options: nosniff
< Alt-Svc: h3-23=":443"; ma=86400
< Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
< Server: cloudflare
< CF-RAY: 533243e4bcd4bbf4-LHR
< 
10个cURL命令用法及实时示例
10个cURL命令用法及实时示例

限速下载

如果您正在进行优化并想查看以特定速度下载需要多长时间,您可以:

 curl –-limit-rate 2000B

原来的:

 curl –-limit-rate 2000B https://gf.dev

使用代理连接

如果您使用的 DMZ 服务器需要使用代理连接到外部世界,则非常有用。

 curl --proxy yourproxy:port https://yoururl.com

插入标头和测试 URL

通过将标头插入数据中,您可以使用curl 来测试或解决特定问题。让我们看一下下面使用 Content-Type 发出请求的示例。

 curl --header 'Content-Type: application/json' http://yoururl.com

运行上面的命令将要求curl在请求标头中将Content-Type作为application/json传递。

仅显示响应标头

如果您正在排除故障并希望快速查看响应标头,可以使用以下语法:

 curl --head http://yoururl.com

原来的:

 [root@gf-lab tmp]# curl --head https://chandan.io
HTTP/1.1 200 OK
Date: Sat, 09 Nov 2019 19:51:23 GMT
Content-Type: text/html
Connection: keep-alive
Set-Cookie: __cfduid=d3cb2c7b8e566ad99c870b0af12b0f1eb1573329083; expires=Sun, 08-Nov-20 19:51:23 GMT; path=/; domain=.chandan.io; HttpOnly
X-GUploader-UploadID: AEnB2Uo96JhvJmR2zYUL-Ndh2ta3UD_ykQAB5C7O8cjZQhCf-GxHQ0MsodSzRnl3guSN3ywAYNjtWcPXfwDXjLg3bQ-P5vQMOA
Expires: Sat, 09 Nov 2019 20:51:23 GMT
Cache-Control: public, max-age=3600
Last-Modified: Mon, 06 Aug 2018 10:45:47 GMT
x-goog-generation: 1533552347482034
x-goog-metageneration: 1
x-goog-stored-content-encoding: identity
x-goog-stored-content-length: 24620
x-goog-hash: crc32c=DpDPAQ==
x-goog-hash: md5=cIP/3rusdUx12Zla1kf1yA==
x-goog-storage-class: MULTI_REGIONAL
Accept-Ranges: bytes
CF-Cache-Status: DYNAMIC
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
CF-RAY: 53325234dc2fbb9a-LHR

[root@gf-lab tmp]# 

连接到 HTTPS/SSL URL 并忽略 SSL 证书错误。

当您尝试访问受 SSL/TLS 证书保护的 URL 时,如果证书不正确或 CN 不匹配,您将收到以下错误:

 curl: (51) Unable to communicate securely with peer: requested domain name does not match the server's certificate.

好消息。您可以使用--insecure标志告诉 cURL 忽略证书错误。

 curl --insecure https://yoururl.com

使用特定协议 (SSL/TLS) 连接

对于测试特定 URL 是否可以通过特定 SSL/TLS 协议进行握手非常有用。

使用 SSL v3 进行连接

curl --sslv3 https://yoururl.com

对于不同的 TLS 版本

curl --tlsv1 https://example.com
curl --tlsv1.0 https://example.com
curl --tlsv1.1 https://example.com
curl --tlsv1.2 https://example.com
curl --tlsv1.3 https://example.com

从 FTP 服务器下载文件

您还可以通过指定用户名和密码使用curl来下载文件。

 curl -u user:password -O ftp://ftpurl/style.css

您始终可以将“ -v ”与任何语法一起使用以在详细模式下打印。

使用主机标头

当仅当主机标头匹配时请求的内容才可用时,主机标头对于通过 IP 测试目标 URL 非常有用。或者,如果您想使用负载均衡器 IP/URL 测试您的应用程序。

 curl --header 'Host: targetapplication.com' https://192.0.0.1:8080/

想尝试在线使用 cURL 吗?

是的,可以使用以下工具。您可以远程运行 cURL。

Online CURL – 在线检索 URL 的轻量级工具,具有附加选项:

 --connect-timeout
--cookie
--data
--header
--head
--location
--max-time
--proxy
--request
--user
--url
--user-agent
   

cURL 命令行生成器– 这是不同的。这可以帮助您构建curl命令,使您可以在漂亮的UI中输入信息,并在底部显示cURL命令。

卷曲生成器
卷曲生成器

cURL 是一个帮助解决实时连接问题的实用程序。我希望以上信息对您有所帮助。如果您想了解更多信息,我们推荐Linux 命令行基础知识在线课程。

浅显易懂的《10个cURL命令用法及实时实例》讲解!您必须观看的 2 个最佳视频

【Linuxコマンド】curlコマンドの実例
https://www.youtube.com/shorts/8zaELXNO2c0
curlコマンドで天気予報を見てみた
https://www.youtube.com/watch?v=i7imSVhdQnw&pp=ygVCIDEwIGNVUkwg44Kz44Oe44Oz44OJ44Gu5L2_55So5rOV44Go44Oq44Ki44Or44K_44Kk44Og44Gu5L6LJmhsPUpB