美文网首页
curl常用参数

curl常用参数

作者: NoStory | 来源:发表于2022-07-26 23:26 被阅读0次

    curl是常用的网络调试工具,特别是HTTP请求用它调试非常方便。

    参数大纲

    参数 说明
    -v 打印头信息
    -X 设置请求方法,其中POST方法经常与-d搭配使用
    -d 设置POST请求体-d 'type=0?enable=1'
    -o 下载文件,如:-o 1.txt。若指定-v -o /dev/null可以值看请求过程方便调试
    -L 自动处理重定向
    -H 设置请求头,如-H "Content-Type: application/json",多个头可增加多个-H参数
    -k 忽略TSL/SSL证书检查
    -s 不打印下载等细节的进度,可以使调试内容更清晰
    --limit-rate 限制带宽
    --resolve 设置请求链接的IP,例如--resolve *:443:127.0.0.1,即所有连接至443端口的请求,都连接至127.0.0.1这台机器

    简单示例

    1. 执行以下命令会将http://baidu.com页面的原始内容打印在控制台
    $ curl -X GET http://baidu.com
    <html>
    <meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
    </html>
    
    1. -X GET告诉curl执行的是GET请求,由于curl默认行为是GET,因此GET请求的-X参数可以省略,如
    $ curl http://baidu.com
    <html>
    <meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
    </html>
    
    1. "http://"也可以省略,省略后如
    $ curl baidu.com
    <html>
    <meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
    </html>
    

    常用参数

    上面3组命令都是将http://baidu.com的页面内容打印在控制台,但要满足真实场景的需要还远远不够。

    -v 打印详情

    使用频率:☆☆☆☆☆
    参数用途:增加-v参数后curl会将执行过程打印在控制台,包括建立tcp连接、TSL/SSL握手、请求头、响应头等信息打印出来,可以很方便的查看请求细节

    $ curl -v baidu.com
    * Rebuilt URL to: baidu.com/
    *   Trying 110.242.68.66...
    * TCP_NODELAY set
    * Connected to baidu.com (110.242.68.66) port 80 (#0)
    > GET / HTTP/1.1
    > Host: baidu.com
    > User-Agent: curl/7.54.0
    > Accept: */*
    > 
    < HTTP/1.1 200 OK
    < Date: Wed, 27 Jul 2022 14:50:52 GMT
    < Server: Apache
    < Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
    < ETag: "51-47cf7e6ee8400"
    < Accept-Ranges: bytes
    < Content-Length: 81
    < Cache-Control: max-age=86400
    < Expires: Thu, 28 Jul 2022 14:50:52 GMT
    < Connection: Keep-Alive
    < Content-Type: text/html
    < 
    <html>
    <meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
    </html>
    * Connection #0 to host baidu.com left intact
    

    -X 修改请求方法

    使用频率:☆☆☆
    -X <METHOD>用于指定请求方法,HTTP协议规定的请求方法有GET、POST、PUT、DELETE、HEAD等,但该参数实际上可以并不对请求是否满足HTTP协议要求。

    $ curl -v -X GET baidu.com # 如前面所述GET请求的-X参数可省
    ... 
    < HTTP/1.1 200 OK
    ...
    
    
    $  curl -v -X POST baidu.com # 该命令只为做演示,因为baidu.com这个地址并不响应POST请求
    ...
    < HTTP/1.1 200 OK
    ...
    
    

    -o 下载文件

    -o FILE
    使用频率:☆☆☆
    参数用途:curl 默认行为是将请求体打印在控制台,但可以通过-o参数指定文件,即下载url到指定路径

    $ curl -o logo.png https://www.baidu.com/img/flexible/logo/pc/result.png
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100  6617  100  6617    0     0  31318      0 --:--:-- --:--:-- --:--:-- 31360
    
    $ file logo.png # file命令可以查看文件格式,可见已经成功将上述logo下载至本地
    logo.png: PNG image data, 202 x 66, 8-bit/color RGBA, non-interlaced
    

    -L 跟踪重定向

    -L
    使用频率:☆☆☆
    参数用途:大部分编程语言的网络封装库均会自动处理http协议的3xx重定向,这对于使用这些编程语言的应用不需要特别关注重定向的执行细节。但curl作为一款调试工具,默认情况下并不会执行重定向请求,这对于调试非常有利。但它也同时提供了自动处理重定向的方法,即增加-L参数。

    $ curl -L -v -o logo.png http://mtw.so/5PRds7 
    > Host: mtw.so
    > User-Agent: curl/7.54.0
    > Accept: */*
    > 
    < HTTP/1.1 302 Move Temporarily
    < Content-Type: text/html;charset=UTF-8
    < Content-Length: 0
    < Connection: keep-alive
    < Location: http://www.baidu.com/img/flexible/logo/pc/result.png
    ... !!! 以下是重定向的请求,如果未增加-L参数到这里就结束了
    *   Trying 110.242.68.3...
    * TCP_NODELAY set
    * Connected to www.baidu.com (110.242.68.3) port 80 (#1)
    > GET /img/flexible/logo/pc/result.png HTTP/1.1
    > Host: www.baidu.com
    > User-Agent: curl/7.54.0
    > Accept: */*
    > 
    < HTTP/1.1 200 OK
    < Accept-Ranges: bytes
    < Cache-Control: max-age=315360000
    < Content-Length: 6617
    < Content-Type: image/png
    ...
    

    相关文章

      网友评论

          本文标题:curl常用参数

          本文链接:https://www.haomeiwen.com/subject/tuzhcltx.html