Skip to content

curl

curl

image-20231016062113861

目录

[toc]

1、命令用途

curl字符界面浏览器。

​ 另一个可以用来进行文件传输的工具是 curl,它是对 libcurl 库的一个命令行工具包装。libcurl 库中提供了相应功能的 API,可以在程序中调用。对于 libcurl 库的使用方法介绍超出了本文的讨论范围。curl 使用 URL 的语法来传输文件,它支持 FTP,FTPS,HTTP,HTTPS,TFTP,SFTP,TELNET 等多种协议。curl 功能强大,它提供了包括代理支持,用户认证,FTP 上载,HTTP post,SSL 连接,文件续传等许多特性。

2、基本语法

bash
curl[options ]<url>

其中下载参数大约有 80 多个,curl 的各个功能完全依靠这些参数来完成。下面举例说明 curl 的一些基本用法。

3、参数含义

bash
-I/—head只显示请求头信息-v,--verboseMaketheoperationmoretalkative#使操作更健谈 -H,--headerLINECustomheadertopasstoserver(H) #自定义头传递给服务器

4、使用案例

范例:执行远程服务器上的脚本

bash
curl-sSfLhttps:daemonset.apps/longhorn-environment-checkcreatedwaitingforpodstobecomeready(0/2)waitingforpodstobecomeready(0/2)allpodsready(2/2)MountPropagationisenabled!cleaningup...daemonset.apps"longhorn-environment-check"deletedcleanupcomplete-f,--failFailsilently(no outputatall) on HTTP errors (H)-L,--locationFollowredirects(H)--location-trustedlike--locationandsendauthtootherhosts(H)-S,--show-errorShowerror.With-s,makecurlshowerrorswhentheyoccur-s,--silentSilentmode.Don't output anything

或者:

bash
curl-Lhttps:NAMETYPECLUSTER-IPEXTERNAL-IPPORT(S) AGEmy-nginxClusterIP10.110.207.185<none>80/TCP25h[root@master1 ~]#curl 10.110.207.185<!DOCTYPEhtml><html><head><title>Welcome to nginx!</title><style>html{color-scheme:lightdark;}body{width:35em;margin:0auto;font-family:Tahoma,Verdana,Arial,sans-serif;}</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page,the nginx web server is successfully installed andworking.Furtherconfigurationisrequired.</p><p>For online documentation and support please refer to<a href="http:Commercialsupportisavailableat<a href="http:<p><em>Thank you forusing nginx.</em></p></body></html>[root@master1 ~]#

案例:curl如何测试一个url是否发生重定向了?

要使用 curl测试一个 URL 是否发生了重定向,你可以通过 -I(或 --head)参数来发送一个 HEAD 请求,这样 curl只会获取响应头部而不会下载页面内容。同时,使用 -L参数可以让 curl跟随服务器的重定向。结合使用 -v参数可以提供详细的过程输出,包括请求和响应的头部信息。

如果你只想看重定向链和最终的 URL,而不跟随重定向,你可以使用 -I参数配合 -L-s参数。-s参数会让 curl运行在 silent 模式,减少输出的冗余信息,但你可能会想结合 -v来看详细的过程。

跟随重定向查看最终目的地

如果想要检测 URL 最终重定向的位置,可以这么做:

bash
curl-L-Ihttp:curl-L-Ihttp:cpdocker-compose-Linux-x86_64/usr/local/bin/docker-composechmod+x/usr/local/bin/docker-compose#测试命令docker-composeDefineandrunmulti-containerapplicationswithDocker.Usage:docker-compose[-f <arg>...][options] [COMMAND] [ARGS...]docker-compose-h|--help

或者不加-o直接使用重定向:

img

案例:

使用 curl命令的时候添加了一个 -L标志,该标志指示 curl将遵循重定向。

bash
$kubectlexec"${SOURCE_POD}"-csleep--curl-sSL-o/dev/null-D-http:# 输出如下结果HTTP/1.1301MovedPermanently# ......location:https:HTTP/2200content-type:text/html;charset=utf-8# ......

📌 案例:docker-compose安装

bash
curl-Lhttps:#添加执行权限chmod+x/usr/local/bin/docker-compose

范例:-v -H参数

bash
curl-vhttp:-v,--verboseMaketheoperationmoretalkative#使操作更健谈 -H,--headerLINECustomheadertopasstoserver(H) #自定义头传递给服务器#查看pod和ingress[root@master1 ingress]#kubectl get ingress my-nginxNAMECLASSHOSTSADDRESSPORTSAGEmy-nginxnginxngdemo.qikqiak.com172.29.9.518023h[root@master1 ingress]#kubectl get po -l app=my-nginxNAMEREADYSTATUSRESTARTSAGEmy-nginx-7c4ff94949-lwvjf1/1Running023h#验证ingress应用[root@master1 ingress]#curl -v http:*About to connect() to 172.29.9.51 port 80 (#0)*Trying172.29.9.51...*Connected to 172.29.9.51 (172.29.9.51) port 80 (#0)>GET/HTTP/1.1>User-Agent:curl/7.29.0>Accept:*/*>Host:ngdemo.qikqiak.com><HTTP/1.1 200 OK<Date:Mon,03 Jan 2022 08:51:03 GMT<Content-Type:text/html<Content-Length:615<Connection:keep-alive<Last-Modified:Tue,28 Dec 2021 15:28:38 GMT<ETag:"61cb2d26-267"<Accept-Ranges:bytes<<!DOCTYPEhtml><html><head><title>Welcome to nginx!</title><style>html{color-scheme:lightdark;}body{width:35em;margin:0auto;font-family:Tahoma,Verdana,Arial,sans-serif;}</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page,the nginx web server is successfully installed andworking.Furtherconfigurationisrequired.</p><p>For online documentation and support please refer to<a href="http:Commercialsupportisavailableat<a href="http:<p><em>Thank you forusing nginx.</em></p></body></html>*Connection #0 to host 172.29.9.51 left intact#或者在浏览器里测试

范例:-I/—head 只显示请求头信息

bash
-I/—head只显示请求头信息-I,--head(HTTP/FTP/FILE) FetchtheHTTP-headeronly!HTTP-serversfeaturethecommandHEADwhichthisusestogetnothingbuttheheaderofadocument.WhenusedonanFTPorFILEfile,curldisplaysthefilesizeandlastmodificationtimeonly.[root@k8s-master ~]#curl -I 10.104.197.77:80 #这个10.104.197.77:80 通过HTTP/1.1200OKServer:nginx/1.21.0Date:Sat,26Jun202109:02:37GMTContent-Type:text/htmlContent-Length:612Last-Modified:Tue,25May202112:28:56GMTConnection:keep-aliveETag:"60aced88-264"Accept-Ranges:bytes[root@k8s-master ~]#curl --head 10.104.197.77:80HTTP/1.1200OKServer:nginx/1.21.0Date:Sat,26Jun202109:02:43GMTContent-Type:text/htmlContent-Length:612Last-Modified:Tue,25May202112:28:56GMTConnection:keep-aliveETag:"60aced88-264"Accept-Ranges:bytes

排错:--data-raw选项报错(已解决) curl发送post数据

  • gpt问答:2023年11月2日

问题:

报错提示

bash
[root@docker 4.envoy]#curl --location --request POST 'http:--header'Content-Type:application/json'--data-raw'{"address":"172.17.0.7","port_value":8081}'curl:option--data-raw:isunknowncurl:try'curl --help'or'curl --manual'formoreinformation

回答:

错误提示表明 --data-raw选项是未知的。在较早版本的 curl中,--data-raw选项可能不可用。你可以尝试使用替代的方法来发送 JSON 数据。以下是一种常见的方法:

bash
curl--location--requestPOST'http:--header 'Content-Type:application/json'\--data '{"address":"172.17.0.7","port_value":8081}'

上述命令使用 --data选项,后跟一个合法的 JSON 字符串。这将向指定的端点发送 JSON 数据。确保你的 curl版本支持这种方式。如果你的 curl版本依然不支持,请尝试升级 curl到较新的版本。

经测试正常:

bash
[root@docker 4.envoy]#curl --location --request POST 'http:>--header 'Content-Type:application/json'\>--data '{>"address":"172.17.0.7",>"port_value":8081>}'{"id":"db8feebe-212e-4161-b5b5-d38217aa3afc"}[root@docker 4.envoy]#

案例:curl如何测试https证书来源极其是否是安全的?

使用 curl命令可以测试 HTTPS 证书的来源以及验证其是否安全。以下是一些方法和选项,你可以通过这些方法来检查 HTTPS 证书的详细信息:

查看证书信息

使用 -v--verbose选项可以查看详细的连接过程,包括证书信息。

sh
curl-vhttps:*subject:CN=example.com*start date:Aug 1 00:00:00 2023 GMT*expire date:Jul 31 23:59:59 2024 GMT*subjectAltName:host "example.com"matched cert's "example.com"*issuer:C=US;O=Let's Encrypt;CN=R3*SSL certificate verify ok.

bash
curl-vhttps:

版权:此文章版权归 One 所有,如有转载,请注明出处!

链接:可点击右上角分享此页面复制文章链接

上次更新时间:

最近更新