暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

CURL常用参数及故障排查

1874

故障排查

    curl --connect-timeout 3  -s -w "http_code:%{http_code}\ncontent_type:%{content_type}\ntime_namelookup:%{time_namelookup}\ntime_redirect:%{time_redirect}\ntime_pretransfer:%{time_pretransfer}\ntime_connect:%{time_connect}\ntime_starttransfer:%{time_starttransfer}\ntime_total:%{time_total}\nspeed_download:%{speed_download}\n" "https://app.example.com/openapi/proxy/iou/xxxxx" -d 'creditor="xxxxxxx"&status=0&openId="xxxx"&accessToken="xxxx"&appId="111111"'
    • http_code  返回http状态码,如200,301,302,503,502等;

    • content_type 显示在Header里面使用Content-Type来表示的具体请求中的媒体类型信息;

    • time_namelookup 从请求开始,到DNS解析完成所使用的时间,经常用来排除DNS解析的问题;

    • time_redirect 重定向的时间;

    • time_pretransfer 从开始到准备传输的时间;

    • time_connect 连接时间,从开始到TCP三次握手完成时间,这里面包括DNS解析的时候,如果想求连接时间,需要减去上面的解析时间;

    • time_starttransfer 开始传输时间,从发起请求开始,到服务器返回第一个字段的时间;

    • time_total 总时间;

    • speed_download 经常使用它来测试网速度,下载速度,单位是字节每秒;

    • size_request 请求头的大小;

    • size_header 下载的header的大小;

      等等


    常用参数

    1. 通过GET方式获取页面内容

      加任何参数,默认是GET请求,把请求链接的内容输出到标准输出;

        curl https://www.baidu.com


      • 查看HTTP头信息或内容

        通过-I(大小的i)可以查看访问链接的HTTP头部信息,通过-i(小写的i可以查看header信息以及链接内容)

          [root@VM_0_13_centos ~]# curl -I https://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, 10 Nov 2019 10:52:05 GMT
          Etag: "575e1f60-115"
          Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
          Pragma: no-cache
          Server: bfe/1.0.8.18


          [root@VM_0_13_centos ~]#



        • 获取重定向之后的内容

          使用-L(大小的l),有的时候我们curl的时候,比如:302跳转,但我们想获取跳转后的内容,使用-L即可获取到;

            $ curl  https://app.example.com
            <html>
            <head><title>302 Found</title></head>
            <body bgcolor="white">
            <center><h1>302 Found</h1></center>
            <hr><center>nginx/1.8.1</center>
            </body>
            </html>
            $
            $ curl -L https://app.example.com
                <meta name="keywords" content="金融,。。。。。。。。。 "images/pc_footer_3_1.jpg";.png";
            $
          •  命令行上传图片

            使用-F选项可以上传图片,并且还可以指定参数,使用-F时,默认在Header头中加入Content-Type: multipart/form-data,将card.jpg做为file上传,MIME 类型默认为application/octet-stream,我们可以手动指定MIME类型为image/jpg,如-F file='@/Users/mactang/Desktop/card.jpg;type=image/jpg',并且-F选项还可以指定上传到服务器的文件名称如-F file='@/Users/mactang/Desktop/card.jpg;type=image/jpg;filename=test.jpg'

              curl https://cloudapi.example.cn/ocr/idcard -F api_id=557c656d71f5xxxxxxxxxx -F api_secret=7ff1xxxxxxxxxaa3033ad252 -F file='@/Users/mactang/Desktop/card.jpg'
              curl https://cloudapi.example.cn/ocr/idcard -F api_id=557c656d71f5xxxxxxxxxx -F api_secret=7ff1xxxxxxxxxaa3033ad252 -F file='@/Users/mactang/Desktop/card.jpg;type=image/jpg;filename=test.jpg'


            • 使用-H自定义Header信息传递到服务器

              除下面的-H "Host: xxxx"外,还有两个经常用的参数:-H "User-Agent: xxx" -H "Cookie: xxxx",可以在curl命令行中写多个-H

                curl -H "Host: www.example.com" http://192.168.1.100


              • POST请求参数-d

                有的时候参数还使用"{}"的形式

                  curl -d "username=admin&passwordd=admin" http://www.example.com/


                • 保存文件

                  使用-o或者-O,区别是是否指定保存的文件名

                    curl -o index.html http://www.example.com/
                    curl -O http://www.example.com/index.html #明确知道是一个文件,而不是一个目录


                  文章转载自Linux点滴运维实践,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                  评论