0
点赞
收藏
分享

微信扫一扫

应用层 curl 用法指南


简介

curl 是常用的命令行工具,用来请求 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。

它的功能非常强大,命令行参数多达几十种。如果熟练的话,完全可以取代 Postman 这一类的图形界面工具。

应用层 curl 用法指南_ci

本文介绍它的主要命令行参数,作为日常的参考,方便查阅。内容主要翻译自《curl cookbook》。为了节约篇幅,下面的例子不包括运行时的输出,初学者可以先看我以前写的《curl 初学者教程》。

不带有任何参数时,curl 就是发出 GET 请求。

$ curl https://www.example.com

上面命令向www.example.com发出 GET 请求,服务器返回的内容会在命令行输出。

-X(请求方法)

-X参数指定 HTTP 请求的方法。

$ curl -X POST https://www.example.com

上面命令对https://www.example.com发出 POST 请求。

-I(打印响应头)

HEAD :请求指定url,获取响应的头信息。 HEAD请求时,服务端并不会返回响应的主体数据,多用于client查看服务端的性能 

-I参数向服务器发出 HEAD 请求,然会将服务器返回的 HTTP 标头打印出来。

$ curl -I https://www.example.com

[root@jenkins ~]# curl -I www.baidu.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Fri, 30 Dec 2022 06:20:54 GMT
Etag: "575e1f60-115"
Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
Pragma: no-cache
Server: bfe/1.0.8.18

[root@jenkins ~]# curl  www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>?惧害涓涓.?浣.氨?ラ.</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=?惧害涓涓.class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>?伴.</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>?板.</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>瑙.?</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>璐村.</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>?诲?</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">?诲?</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">?村?浜у.</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>?充??惧害</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>浣跨.?惧害?.?璇?/a>  <a href=http://jianyi.baidu.com/ class=cp-feedback>?.??.?</a> 浜.CP璇.30173??nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

上面命令输出服务器对 HEAD 请求的回应。 --head参数等同于-I

$ curl --head https://www.example.com

[root@192 ~]# curl --head  www.baidu.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Sun, 29 Jan 2023 15:26:36 GMT
Etag: "575e1f74-115"
Last-Modified: Mon, 13 Jun 2016 02:50:28 GMT
Pragma: no-cache
Server: bfe/1.0.8.18

-i

-i参数打印出服务器回应的 HTTP 标头。

$ curl -i https://www.example.com

上面命令收到服务器回应后,先输出服务器回应的标头,然后空一行,再输出网页的源码。

-H

-H参数添加 HTTP 请求的标头。

$ curl -H 'Accept-Language: en-US' https://google.com

上面命令添加 HTTP 标头Accept-Language: en-US

$ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com

上面命令添加两个 HTTP 标头。

$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login

上面命令添加 HTTP 请求的标头是Content-Type: application/json,然后用-d参数发送 JSON 数据。

curl --location --request GET 'http://192.168.11.129/api/v4/projects/2/repository/branches' \
--header 'PRIVATE-TOKEN: gL8YqsRqzmjfnZ34N7UR'

获取gitlab的分支。

为简易认证绑定AppCode_API网关 APIG_用户指南_凭据管理_华为云简易认证指调用API时,在HTTP请求头部消息增加一个参数X-Apig-AppCode(参数值填凭据详情中“AppCode”的值),而不需要对请求内容签名,API网关也仅校验AppCode,不校验请求签名,从而实现快速响应。当使用APP认证,且开启了简易认证模式,API请求既可以选择使用key和Secret做签名和校验,也可以选择使用Aphttps://support.huaweicloud.com/usermanual-apig/apig_03_0058.html

简易认证指调用API时,在HTTP请求头部消息增加一个参数X-Apig-AppCode(参数值填凭据详情中“AppCode”的值),而不需要对请求内容签名,API网关也仅校验AppCode,不校验请求签名,从而实现快速响应。

当使用APP认证,且开启了简易认证模式,API请求既可以选择使用key和Secret做签名和校验,也可以选择使用AppCode进行简易认证。

使用AppCode进行API请求的简易认证

  1. 在创建API时,选择“APP认证”并且开启“支持简易认证”。
    说明:如果您修改已有API为简易认证,需要在修改完成后,将API重新发布,使简易认证模式生效。
  2. 将支持简易认证的API绑定到已创建的凭据。
  3. 发送请求时,增加请求头部参数“X-Apig-AppCode”,省略请求签名相关信息。
    以Curl方式为例,增加头部参数名称:X-Apig-AppCode,参数值填已生成的AppCode。

curl -X GET "https://api.exampledemo.com/testapi" 
-H "content-type: application/json"  
-H "host: api.exampledemo.com" 
-H "X-Apig-AppCode: xhrJVJKABSOxc7d***********FZL4gSHEXkCMQC"

-d

-d参数用于发送 POST 请求的数据体。

$ curl -d'login=emma&password=123'-X POST https://google.com/login # 或者 $ curl -d 'login=emma' -d 'password=123' -X POST https://google.com/login

使用-d参数以后,HTTP 请求会自动加上标头Content-Type : application/x-www-form-urlencoded。并且会自动将请求转为 POST 方法,因此可以省略-X POST

-d参数可以读取本地文本文件的数据,向服务器发送。

$ curl -d '@data.txt' https://google.com/login

上面命令读取data.txt文件的内容,作为数据体向服务器发送。

[root@jenkins ~]# curl -s -d "script=$(cat create_agent.groovy)"   --header "Jenkins-Crumb:f72990e2bc09a468effa69ec41f1432844bc709ca5ad6130d600da24ad16672a"   -X  POST http://admin:115fcd2e2d30505df7a18f23763c1332a0@192.168.11.128:8080/scriptText

Result: {
"jenkinsUrl" : "http://192.168.11.128:8080", 
"jenkinsHome": "/opt/jenkins",
"computerUrl": "computer/%5F%5FAGENT%5FNAME%5F%5F",
"computerSecret": "6473dea92efa350d744450b91f010cd01880e0686597b8d33ef0876e698127c7"
}

-u

-u参数用来设置服务器认证的用户名和密码。

$ curl -u 'bob:12345' https://google.com/login

上面命令设置用户名为bob,密码为12345,然后将其转为 HTTP 标头Authorization: Basic Ym9iOjEyMzQ1

应用层 curl 用法指南_HTTP_02

[root@192 ~]# curl -u 'admin:123456' http://192.168.11.128:8080/job/Test/job/webhook-01/api/json 
{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowJob","actions":[{},{},{},{},{},{},{},{"_class":"org.jenkinsci.plugins.displayurlapi.actions.JobDisplayAction"},{},{},{"_class":"com.cloudbees.plugins.credentials.ViewCredentialsAction"}],"description":"","displayName":"webhook-01","displayNameOrNull":null,"fullDisplayName":"Test 禄 webhook-01","fullName":"Test/webhook-01","name":"webhook-01","url":"http://192.168.11.128:8080/job/Test/job/webhook-01/","buildable":true,"builds":[{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":13,"url":"http://192.168.11.128:8080/job/Test/job/webhook-01/13/"}],"color":"blue","firstBuild":{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":13,"url":"http://192.168.11.128:8080/job/Test/job/webhook-01/13/"},"healthReport":[{"description":"Build stability: No recent builds failed.","iconClassName":"icon-health-80plus","iconUrl":"health-80plus.png","score":100}],"inQueue":false,"keepDependencies":false,"lastBuild":{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":13,"url":"http://192.168.11.128:8080/job/Test/job/webhook-01/13/"},"lastCompletedBuild":{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":13,"url":"http://192.168.11.128:8080/job/Test/job/webhook-01/13/"},"lastFailedBuild":null,"lastStableBuild":{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":13,"url":"http://192.168.11.128:8080/job/Test/job/webhook-01/13/"},"lastSuccessfulBuild":{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":13,"url":"http://192.168.11.128:8080/job/Test/job/webhook-01/13/"},"lastUnstableBuild":null,"lastUnsuccessfulBuild":null,"nextBuildNumber":14,"property":[{"_class":"jenkins.model.BuildDiscarderProperty"},{"_class":"org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty"}

 curl 能够识别 URL 里面的用户名和密码。

$ curl https://bob:12345@google.com/login

上面命令能够识别 URL 里面的用户名和密码,将其转为上个例子里面的 HTTP 标头。

$ curl -u 'bob' https://google.com/login

上面命令只设置了用户名,执行后,curl 会提示用户输入密码。

-k

-k参数指定跳过 SSL 检测。

$ curl -k https://www.example.com

上面命令不会检查服务器的 SSL 证书是否正确。

在发送https请求时,加上-k参数,否则会有如下证书的报错

curl: (60) Peer's certificate issuer has been marked as not trusted by the user.
More details here: http://curl.haxx.se/docs/sslcerts.html
 

-o

-o参数将服务器的回应保存成文件,等同于wget命令。

$ curl -o example.html https://www.example.com

上面命令将www.example.com保存成example.html

nexus 下载制品

curl -u admin:admin123 http://192.168.1.200:8081/repository/anyops/com/anyops/a
nyops-devops-service/1.1.1/anyops-devops-service-1.1.1.jar -o anyops-devops-service-1.1.1.jar


[root@devops tmp]# curl -u admin:123456 http://121.40.102.116:8081/repository/devops-maven-snapshot/com/devops/demo/1.1.1-SNAPSHOT/demo-1.1.1-20230117.164103-1.jar  -o anyops-devops-service-1.1.1.jar
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 8650k  100 8650k    0     0   740k      0  0:00:11  0:00:11 --:--:--  714k



[root@devops tmp]# ls
AliyunAssistClientSingleLock.lock
anyops-devops-service-1.1.1.jar
systemd-private-f4e6c83202c94b65be3207dc4a0e9885-chronyd.service-LAAK7T
[root@devops tmp]#

-O

-O参数将服务器回应保存成文件,并将 URL 的最后部分当作文件名。

$ curl -O https://www.example.com/foo/bar.html

上面命令将服务器回应保存成文件,文件名为bar.html

-A

-A参数指定客户端的用户代理标头,即User-Agent。curl 的默认用户代理字符串是curl/[version]

$ curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' https://google.com

上面命令将User-Agent改成 Chrome 浏览器。

$ curl -A '' https://google.com

上面命令会移除User-Agent标头。

也可以通过-H参数直接指定标头,更改User-Agent

$ curl -H 'User-Agent: php/1.0' https://google.com

通过curl命令访问K8s API

 使用token

token="eyJhbGciOiJSUzI1NiIsI..."
curl --cacert /etc/kubernetes/pki/ca.crt -H "Authorization: Bearer $token"  https://192.168.31.61:6443/api/v1/namespaces/default/pods

 使用证书

curl https://192.168.11.135:6443/api/v1/nodes \
--cacert /etc/kubernetes/pki/ca.crt \
--cert /etc/kubernetes/pki/apiserver-kubelet-client.crt \
--key /etc/kubernetes/pki/apiserver-kubelet-client.key

举报

相关推荐

0 条评论