作用
curl 是常用的命令行工具,用来请求 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。
它的功能非常强大,命令行参数多达几十种。如果熟练的话,完全可以取代 Postman 这一类的图形界面工具。
语法
最简单语法,发送一个get请求
curl https://www.baidu.com/
所以基本语法是:
curl 参数 url
参数详解
-X 指定请求方式:post/get
1、语法
curl -X POST url
2、demo
curl -X POST https://www.baidu.com/
curl -X PUT https://www.baidu.com/
curl -X DELETE https://www.baidu.com/
curl -X GET https://www.baidu.com/
-H/–header 指定请求头:即header里面的内容
1、语法
curl -H "header里面的key和value" url
2、demo
指定token
curl -H 'Token:XXX' https://www.baidu.com/
curl --header 'Token:XXX' https://www.baidu.com/
指定接收参数格式:
curl -H 'Content-Type:application/json' https://www.baidu.com/
curl --header 'Content-Type:application/json' https://www.baidu.com/
指定d多个:
curl -H 'Token:XXX' -H 'Content-Type:application/json' https://www.baidu.com/
-d 指定post请求的参数
1、语法
curl -d'参数'-X POST https://www.baidu.com/
注意:-d和参数之间是没有空格的
2、demo
curl -d'login=emma&password=123'-X POST https://www.baidu.com/
curl -d'{"sheetNo":"a6871eda-df53-42ec-ab7c-182874129b38","workStatus":"结束"}'-X POST https://www.baidu.com/
data-urlencode
参数等同于-d,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码。
发送的数据hello world之间有一个空格,需要进行 URL 编码。
curl --data-urlencode 'comment=hello world' https://www.baidu.com/login
-A
指定客户端的用户代理标头,即User-Agent。curl 的默认用户代理字符串是curl/[version]。
将User-Agent改成 Chrome 浏览器。
curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' https://www.baidu.com
移除User-Agent标头。
curl -A '' https://www.baidu.com
等价语法:
curl -H 'User-Agent: php/1.0' https://www.baidu.com
-b
参数用来向服务器发送 Cookie。
生成一个标头Cookie: foo=bar,向服务器发送一个名为foo、值为bar的 Cookie。
curl -b 'foo=bar' https://baidu.com
发送两个Cookie
curl -b 'foo1=bar;foo2=bar2' https://www.baidu.com
读取本地文件cookies.txt,里面是服务器设置的 Cookie(参见-c参数),将其发送到服务器。
curl -b cookies.txt https://www.baidu.com
-c
参数将服务器设置的 Cookie 写入一个文件。
将服务器的 HTTP 回应所设置 Cookie 写入文本文件cookies.txt。
curl -c cookies.txt https://www.google.com
-e
用来设置 HTTP 的标头Referer,表示请求的来源
将Referer标头设为https://google.com?q=example。
curl -e 'https://google.com?q=example' https://www.example.com
等价语法
curl -H 'Referer: https://google.com?q=example' https://www.example.com
-F
用来向服务器上传二进制文件。
给 HTTP 请求加上标头Content-Type: multipart/form-data,然后将文件photo.png作为file字段上传。
curl -F 'file=@photo.png' https://google.com/profile
指定 MIME 类型为image/png,否则 curl 会把 MIME 类型设为application/octet-stream。
curl -F 'file=@photo.png;type=image/png' https://google.com/profile
原始文件名为photo.png,但是服务器接收到的文件名为me.png。
curl -F 'file=@photo.png;filename=me.png' https://google.com/profile
-G
构造 URL 的查询字符串。
发出一个 GET 请求,实际请求的 URL 为https://google.com/search?q=kitties&count=20。如果省略--G,会发出一个 POST 请求
curl -G -d 'q=kitties' -d 'count=20' https://google.com/search
-i
参数打印出服务器回应的 HTTP 标头。
收到服务器回应后,先输出服务器回应的标头,然后空一行,再输出网页的源码
curl -i https://www.example.com
-I/–head
向服务器发出 HEAD 请求,然会将服务器返回的 HTTP 标头打印出来。
输出服务器对 HEAD 请求的回应。
curl -I https://www.baidu.com
curl --head https://www.baidu.com
-k
参数指定跳过 SSL 检测。
不会检查服务器的 SSL 证书是否正确。
curl -k https://www.baidu.com
-L
会让 HTTP 请求跟随服务器的重定向。curl 默认不跟随重定向。
curl -L -d 'tweet=hi' https://api.twitter.com/tweet
–limit-rate
用来限制 HTTP 请求和回应的带宽,模拟慢网速的环境。
将带宽限制在每秒 200K 字节。
curl --limit-rate 200k https://www.baidu.com
###-o
将服务器的回应保存成文件,等同于wget命令
将www.baidu.com保存成baidu.html。
curl -o baidu.html https://www.baidu.com
-O
将服务器回应保存成文件,并将 URL 的最后部分当作文件名。
将服务器回应保存成文件,文件名为bar.html。
curl -O https://www.baidu.com/foo/bar.html
-s
将不输出错误和进度信息
命令一旦发生错误,不会显示错误信息。不发生错误的话,会正常显示运行结果
curl -s https://www.baidu.com
想让 curl 不产生任何输出,可以使用下面的命令。
curl -s -o /dev/null https://www.baidu.com
-S
指定只输出错误信息,通常与-s一起使用。
没有任何输出,除非发生错误。
curl -s -o /dev/null https://www.baidu.com
-u
用来设置服务器认证的用户名和密码。
设置用户名为bob,密码为12345,然后将其转为 HTTP 标头Authorization: Basic Ym9iOjEyMzQ1
curl -u 'bob:12345' https://www.baidu.com/login
只设置了用户名,执行后,curl 会提示用户输入密码
curl -u 'bob' https://www.baidu.com/login
-v
输出通信的整个过程,用于调试
curl -v https://www.baidu.com
–trace
可以用于调试,还会输出原始的二进制数据。
curl --trace - https://www.baidu.com
-x
指定 HTTP 请求的代理。
指定 HTTP 请求通过myproxy.com:8080的 socks5 代理发出。
curl -x socks5://james:cats@myproxy.com:8080 https://www.baidu.com
版权声明:本文为beixiaohaizi原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。