curl: 使用教程



curl

0. 什么是curl?

官方介绍:“Curl is a command-line tool for transferring data specified with URL syntax”。其实就是一个用来发送URL请求的命令行工具。

参考文档:

1. curl基本介绍

1.1 options

–ciphers中可以使用的加密算法参考

详细变量说明,见curl.se

2. curl用法示例

2.1 post用法

2.1.1 post 501 错误

curl -X post -d "{}" https://something.com
HTTP Status 501 – Not Implemented

基于mozilla 501 error docs,501错误是请求的方法不被服务器接受,定睛一看,post不应该是小写,换成POST就好了

2.1.2 post data 格式

# form 格式
curl -d "param1=value1&param2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/data

# json格式
curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/data

curl post docs

2.2 -w用法

使用-w来排查连接的各种时间

curl -o /dev/null -s -w 'time_namelookup: %{time_namelookup}\ntime_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n' https://www.baidu.com
time_namelookup: 0.010346
time_connect: 0.051382
time_starttransfer: 0.251428
time_total: 0.251855