curl的用法记录

2017/10/9


1、http请求跳转
[root@n86 bin]# curl -I -L 10.50.200.32
HTTP/1.1 302 FOUND
Server: nginx/1.6.2
Date: Fri, 25 Dec 2015 02:43:18 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
X-Frame-Options: SAMEORIGIN
Location: http://10.50.200.32/charade/

HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Fri, 25 Dec 2015 02:43:18 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Vary: Cookie
X-Frame-Options: SAMEORIGIN
Set-Cookie: csrftoken=5dY37iQgcjQnYh1jZdrxptyIkTfUh3Rr; expires=Fri, 23-Dec-2016 02:43:18 GMT; Max-Age=31449600; Path=/


2、https请求GET和PUT
使用证书  curl -s --cacert ca.crt
忽略证书  curl -s -k
curl -s -k -u "user:passwd" -H "Content-type: application/xml" -X GET https://abc.com/api/vms

curl -s -k \
-u "user:passwd" \
-H "Content-type: application/xml" \
-d "
<vm>
<name>test03</name>
<cluster><name>Host-Only</name></cluster>
<template><name>centos6u5x64-small-01</name></template>
</vm>
" \
"https://e01.test/api/vms"



3、http状态
hostname=www.qq.com

1)得到http status
[root@svr200-22 ~]# curl -o /dev/null -s -w %{http_code} ${hostname}
200


2)得到详细的 http headers 信息
[root@svr200-22 ~]# curl -o /dev/null -v http://${hostname}
* About to connect() to www.qq.com port 80 (#0)
*   Trying 14.17.32.211... connected
* Connected to www.qq.com (14.17.32.211) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: www.qq.com
> Accept: */*
> 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0< HTTP/1.1 200 OK
< Server: squid/3.4.1
< Date: Wed, 15 Apr 2015 05:54:51 GMT
< Content-Type: text/html; charset=GB2312
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Accept-Encoding
< Expires: Wed, 15 Apr 2015 05:55:51 GMT
< Cache-Control: max-age=60
< Vary: Accept-Encoding
< X-Cache: HIT from shenzhen.qq.com
< 
{ [data not shown]
100  582k    0  582k    0     0  5573k      0 --:--:-- --:--:-- --:--:-- 6544k* Connection #0 to host www.qq.com left intact

* Closing connection #0



 
3)指定一个目标主机IP(此处以从dns中得到的一个解析结果为例),得到 http headers 信息:
[root@svr200-22 ~]# nslookup ${hostname} |grep Address |grep -v "#53$" |cut -d " " -f2 |head -n 1 |xargs -i curl -I -H "Host:${hostname}" http://{}/ 
HTTP/1.1 200 OK
Server: squid/3.4.1
Date: Wed, 15 Apr 2015 05:53:16 GMT
Content-Type: text/html; charset=GB2312
Connection: keep-alive
Vary: Accept-Encoding
Expires: Wed, 15 Apr 2015 05:54:16 GMT
Cache-Control: max-age=60
Vary: Accept-Encoding
X-Cache: HIT from shenzhen.qq.com


4)指定一个目标主机IP(此处以从dns中得到的一个解析结果为例),得到详细的 http headers 信息:
[root@svr200-22 ~]# nslookup ${hostname} |grep Address |grep -v "#53$" |cut -d " " -f2 |head -n 1 |xargs -i curl -o /dev/null -v -H "Host:${hostname}" http://{}/
* About to connect() to 14.17.32.211 port 80 (#0)
*   Trying 14.17.32.211... connected
* Connected to 14.17.32.211 (14.17.32.211) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Accept: */*
> Host:www.qq.com
> 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0< HTTP/1.1 200 OK
< Server: squid/3.4.1
< Date: Wed, 15 Apr 2015 05:53:46 GMT
< Content-Type: text/html; charset=GB2312
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Accept-Encoding
< Expires: Wed, 15 Apr 2015 05:54:46 GMT
< Cache-Control: max-age=60
< Vary: Accept-Encoding
< X-Cache: MISS from shenzhen.qq.com
< 
{ [data not shown]
100  582k    0  582k    0     0  5349k      0 --:--:-- --:--:-- --:--:-- 5546k* Connection #0 to host 14.17.32.211 left intact

* Closing connection #0


5)得到请求响应时间
[root@svr200-22 ~]# curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" ${hostname}
time_connect: 0.046
time_starttransfer: 0.078
time_total: 0.078



4、curl GET 请求中传递参数为 json 格式,且需要传递变量进去时,如何处理转义的问题
有3种方式来处理:

1)转义参数中的: {} [] " 
curl -s \
    --unix-socket /var/run/docker.sock \
    "http:/services?filters=\{\"name\":\[\"${service_name}\"\]\}" \
    |jq '.'

2)转义参数中的: url外层不用""包含,内层使用 '' 来限制。
curl -s \
    --unix-socket /var/run/docker.sock \
    http:/services?filters='\{"name":\["'${service_name}'"\]\}' \
    |jq '.'
    
3)使用 curl -g 参数来允许 URL 中使用  {}[] 而不用转义
       -g, --globoff
              This option switches off the "URL globbing parser". When you set this option, you can specify URLs that contain the letters {}[] without  having  them  being  inter‐
              preted by curl itself. Note that these letters are not normal legal URL contents but they should be encoded according to the URI standard.


实例
~]$ curl "127.0.0.1:10001/services?filters=\{\"name\":\[\"${service_name}\"\]\}"
GET /services?filters={"name":["t001"]} HTTP/1.1

~]$ curl 127.0.0.1:10001/services?filters='\{"name":\["'${service_name}'"\]\}'
GET /services?filters={"name":["t001"]} HTTP/1.1
    
~]$ curl -g 127.0.0.1:10001/services?filters='{"name":["'${service_name}'"]}'
GET /services?filters={"name":["t001"]} HTTP/1.1