curl

用来请求web服务器,名称的含义就是 client 的url,客户端的url工具

不带任何参数,即发送get请求

访问本地的httpd服务
curl 127.0.0.1
服务器的返回内容会在命令行显示

-A指定客户端的用户代理,即user agent ,默认使用的用户代理字符串是curl/[version]

$ 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://google.com
移除useragent,可以使用空字符串,也可以使用-H来直接指定,
curl -A '' https://google.com
curl -H 'User-Agent: php/1.0' https://google.com

-b 向服务器发送cookie

curl -b 'foo=bar' https://google.com
会生成一个请求头,Cookie:foo=bar。给服务器发送一个名为foo,值为bar的Cookie

发送两个cookie
curl -b 'foo1=bar' -b 'foo2=baz' https://google.com
读取本地文件存储的cookie,并发送到服务器
curl -b cookies.txt https://www.google.com

-c 将服务器设置的cookie写入到一个文件保存起来

curl -c cookies.txt https://www.google.com

-d 发送post请求的数据体

curl -d'login=emma&password=123'-X POST https://google.com/login
curl -d 'login=emma' -d 'password=123' -X POST  https://google.com/login
使用-d之后,HTTP 请求会自动加上标头 Content-Type : application/x-www-form-urlencoded。并且会自动将请求转为 POST 方法,因此可以省略 -X POST

还可以读取本地文件的内容,并作为数据体发送到服务器
curl -d '@data.txt' https://google.com/login

--data-urlencode等同于-d,发送post的数据体,区别在于会自动将数据体进行url编码

curl --data-urlencode 'comment=hello world' https://google.com/login 在发送的数据体中有一个空格,需要进行url编码

-e 设置http的请求头referer,表示请求的来源

curl -e 'https://google.com?q=example' https://www.example.com

curl -H 'Referer: https://google.com?q=example' https://www.example.com

-F 向服务器上传二进制文件。

curl -F 'file=@photo.png' https://google.com/profile会给 HTTP 请求加上标头 Content-Type: multipart/form-data,然后将文件 photo.png 作为 file 字段上传。

还可以指定NAME类型
curl -F 'file=@photo.png;type=image/png' https://google.com/profile指定 MIME 类型为 image/png,否则 curl 会把 MIME 类型设为 application/octet-stream。

指定文件名
curl -F 'file=@photo.png;filename=me.png' https://google.com/profile

-G 构造URL的查询字符串

curl -G -d 'q=kitties' -d 'count=20' https://google.com/search
会发出一个 GET 请求,实际请求的 URL 为 https://google.com/search?q=kitties&count=20。如果省略 --G,会发出一个 POST 请求。
如果数据需要 URL 编码,可以结合 --data--urlencode 参数。
curl -G --data-urlencode 'comment=hello world' https://www.example.com

-H 添加 HTTP 请求的请求头

curl -H 'Accept-Language: en-US' https://google.com
添加两个 HTTP 标头。
curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com
添加 HTTP 请求的标头是 Content-Type: application/json,然后用 -d 参数发送 JSON 数据。
$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login

-i 打印出服务器回应的 HTTP 标头。

curl -i https://www.example.com
先输出服务器回应的标头,然后空一行,再输出网页的源码。

-I 向服务器发出 HEAD 请求,然会将服务器返回的 HTTP 标头打印出来。

curl -I https://www.example.com 等同于 --head
curl --head https://www.example.com

-k 跳过SSL检测

curl -k https://www.example.com 不会检查服务器的SSL证书是否正确

-L 让 HTTP 请求跟随服务器的重定向。curl 默认不跟随重定向

curl -L -d 'tweet=hi' https://api.twitter.com/tweet

--limit-rate 限制 HTTP 请求和回应的带宽,模拟慢网速的环境。

将带宽限制在每秒200K字节
curl --limit-rate 200k https://google.com

-o

将服务器的回应保存成文件,等同于 wget 命令。
将 www.example.com 保存成 example.html。
curl -o example.html https://www.example.com

-O 将服务器回应保存成文件,并将 URL 的最后部分当作文件名。(跟o的区别)

curl -O https://www.example.com/foo/bar.html 将服务器回应保存成文件,文件名为 bar.html。

-s 将不输出错误和进度信息。

curl -s https://www.example.com
上面命令一旦发生错误,不会显示错误信息。不发生错误的话,会正常显示运行结果。

如果想让 curl 不产生任何输出,可以使用下面的命令。 curl -s -o /dev/null https://google.com

-S 只输出错误信息,通常与 -o 一起使用。

curl -s -o /dev/null https://google.com 这串命令正常运行的话没有任何输出,除非发生错误

-u 设置服务器认证的用户名和密码。

curl -u 'bob:12345' https://google.com/login
设置用户名为 bob,密码为 12345,然后将其转为 HTTP 标头 Authorization: Basic Ym9iOjEyMzQ1。
curl 能够识别 URL 里面的用户名和密码。 curl https://bob:12345@google.com/login

curl -u 'bob' https://google.com/login 只设置了用户名,执行后,curl 会提示用户输入密码。

-v 输出通信的整个过程,用于调试。

curl -v https://www.example.com
--trace 参数也可以用于调试,还会输出原始的二进制数据。 curl --trace - https://www.example.com

-x 指定 HTTP 请求的代理

curl -x socks5://james:cats@myproxy.com:8080 https://www.example.com
指定 HTTP 请求通过 myproxy.com:8080 的 socks5 代理发出
如果没有指定代理协议,默认为 HTTP。
curl -x james:cats@myproxy.com:8080 https://www.example.com 请求的代理使用 HTTP 协议。

-X 指定 HTTP 请求的方法。

curl -X POST https://www.example.comhttps://www.example.com 发出 POST 请求。

原文地址:https://www.cnblogs.com/cizao/p/11617284.html