curl命令,知多少

curl

简介

cURL是一个利用URL语法在命令行下工作的文件传输工具,用来请求 Web 服务器。

常用示例

  • 查看网页源码 curl www.sina.com
  • 保存到文件 curl -o sina.html www.sina.com
  • 显示显示http response头信息 curl -i www.sina.com
  • 只显示头信息,不显示内容 curl -I www.sina.com
  • 显示通信过程 curl -v www.sina.com
  • 更详细的过程 curl --trace output.txt www.sina.com
  • GET方法 curl example.com/form.cgi?data=xxx&aa=xx
  • POST方法 curl -X POST --data "data=xxx" example.com/form.cgi
  • User Agent字段 curl --user-agent "[User Agent]" [URL]
  • cookie curl --cookie "name=xxx" www.example.com
  • 添加头信息 curl --header "Content-Type:application/json" http://example.com
  • 认证 curl --user name:password example.com
  • 添加解析 curl http://www.example.com --resolve www.example.com:80:127.0.0.1 --resolve www.example.com:443:127.0.0.1
  • 跳过证书检测 curl -k https://www.example.com
bash 复制代码
$ curl  -H 'Content-Type: application/json' -X POST http://localhost:1055/v1/api/notify/ -d '{"hostname":"xcx"}'

$ curl -XPOST $server/nlb?Action=CreateLoadBalancer&Version=2017-12-05 \
    -H 'Content-Type: application/json' \
    -H 'X-Product-Id: xx' \
    -d '{
         "name": "xx",
         "Description": "xx",
      }'
  • 指定打印信息内容
shell 复制代码
# time_namelookup:DNS 域名解析的耗时
# time_connect:TCP 连接建立的时间,就是三次握手的时间
# time_appconnect:SSL/SSH 等上层协议建立连接的时间,比如 connect/handshake 的时间
# time_redirect:从开始到最后一个请求事务的时间
# time_pretransfer:从请求开始到响应开始传输的时间
# time_starttransfer:从请求开始到第一个字节将要传输的时间
# time_total:这次请求花费的全部时间
$ cat curl-format.txt
    time_namelookup:  %{time_namelookup}\n
       time_connect:  %{time_connect}\n
    time_appconnect:  %{time_appconnect}\n
      time_redirect:  %{time_redirect}\n
   time_pretransfer:  %{time_pretransfer}\n
 time_starttransfer:  %{time_starttransfer}\n
                    ----------\n
         time_total:  %{time_total}\n
#-w:从文件中读取要打印信息的格式
#-o /dev/null:把响应的内容丢弃,因为我们这里并不关心它,只关心请求的耗时情况
#-s:不要打印进度条
$ curl -w "@curl-format.txt" -o /dev/null -s https://www.baidu.com
    time_namelookup:  0.002
       time_connect:  0.012
    time_appconnect:  0.037
      time_redirect:  0.000
   time_pretransfer:  0.037
 time_starttransfer:  0.049
                    ----------
         time_total:  0.049

一个实用的curl脚本,帮助我们更好的分析链路耗时情况

shell 复制代码
#!/bin/bash
#
# curl wrapper returning timing information.
#
# curl format adapted from
#  http://josephscott.org/archives/2011/10/timing-details-with-curl/
#
# Example usage:
#   $ curlt http://www.apple.com
#   $ time curlt http://www.apple.com -v

set -e

curl_format='{
 "time_namelookup": %{time_namelookup},
 "time_connect": %{time_connect},
 "time_appconnect": %{time_appconnect},
 "time_pretransfer": %{time_pretransfer},
 "time_redirect": %{time_redirect},
 "time_starttransfer": %{time_starttransfer},
 "time_total": %{time_total}
}'

exec curl -w "$curl_format" -o /dev/null -s "$@"
相关推荐
予枫的编程笔记9 小时前
【Linux进阶篇】从基础到实战:grep高亮、sed流编辑、awk分析,全场景覆盖
linux·sed·grep·awk·shell编程·文本处理三剑客·管道命令
Sheep Shaun9 小时前
揭开Linux的隐藏约定:你的第一个文件描述符为什么是3?
linux·服务器·ubuntu·文件系统·缓冲区
Tfly__9 小时前
在PX4 gazebo仿真中加入Mid360(最新)
linux·人工智能·自动驾驶·ros·无人机·px4·mid360
陈桴浮海9 小时前
【Linux&Ansible】学习笔记合集二
linux·学习·ansible
生活很暖很治愈9 小时前
Linux——环境变量PATH
linux·ubuntu
?re?ta?rd?ed?9 小时前
linux中的调度策略
linux·运维·服务器
深圳市九鼎创展科技9 小时前
瑞芯微 RK3399 开发板 X3399 评测:高性能 ARM 平台的多面手
linux·arm开发·人工智能·单片机·嵌入式硬件·边缘计算
hweiyu0010 小时前
Linux 命令:tr
linux·运维·服务器
Trouvaille ~10 小时前
【Linux】应用层协议设计实战(一):自定义协议与网络计算器
linux·运维·服务器·网络·c++·http·应用层协议
allway210 小时前
基于华为taishan200服务器、arm架构kunpeng920 cpu的虚拟化实战
linux·运维·服务器