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 "$@"
相关推荐
馨谙14 分钟前
shell编程三剑客------sed流编辑器基础应用大全以及运行示例
linux·运维·编辑器
驱动探索者21 分钟前
Linux list 设计
linux·运维·list
遇见火星32 分钟前
在Linux中使用parted对大容量磁盘进行分区详细过程
linux·运维·网络·分区·parted
yuyousheng42 分钟前
CentOS7更换为阿里源
linux·c语言
微露清风1 小时前
系统性学习Linux-第一讲-Linux基础指令
java·linux·学习
zl_dfq2 小时前
Linux 之 【日志】(实现一个打印日志的类)
linux
EmbedLinX2 小时前
一文理解后端核心概念:同步/异步、阻塞/非阻塞、进程/线程/协程
linux·服务器·c语言·网络
zhangrelay2 小时前
linux下如何通过与AI对话设置thinkpad电池充电阈值
linux·运维·笔记·学习
小王努力学编程2 小时前
LangChain——AI应用开发框架(核心组件2)
linux·服务器·c++·人工智能·python·langchain·信号
郝学胜-神的一滴3 小时前
深入理解TCP协议:数据格式与核心机制解析
linux·服务器·网络·c++·网络协议·tcp/ip