-A
-A 参数指定客户端的用户代理标头,即 User-Agent 。curl 的默认用户代理字符串是 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
-b
-b 参数用来向服务器发送 Cookie。
$ curl -b 'foo=bar' https://google.com
-d
-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 。
-d参数可以读取本地文本文件的数据,向服务器发送。
$ curl -d '@data.txt' https://google.com/login
上面命令读取 data.txt 文件的内容,作为数据体向服务器发送。
—data-urlencode
--data-urlencode 参数等同于 -d ,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码。
$ curl --data-urlencode 'comment=hello world' https://google.com/login
上面代码中,发送的数据 hello world 之间有一个空格,需要进行 URL 编码。
-F
-F 参数用来向服务器上传二进制文件。
$ curl -F 'file=@photo.png' https://google.com/profile
上面命令会给 HTTP 请求加上标头 Content-Type: multipart/form-data ,然后将文件 photo.png 作为 file 字段上传。
-H
-H 参数添加 HTTP 请求的标头。
$ curl -H 'Accept-Language: en-US' https://google.com
$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login
上面命令添加 HTTP 请求的标头是 Content-Type: application/json ,然后用 -d 参数发送 JSON 数据。
-i
-i 参数打印出服务器回应的 HTTP 标头。
$ curl -i https://www.example.com
上面命令收到服务器回应后,先输出服务器回应的标头,然后空一行,再输出网页的源码。
-L
-L 参数会让 HTTP 请求跟随服务器的重定向。curl 默认不跟随重定向。
$ curl -L -d 'tweet=hi' https://api.twitter.com/tweet
-o
-o 参数将服务器的回应保存成文件,等同于 wget 命令。
$ curl -o example.html https://www.example.com
上面命令将 www.example.com 保存成 example.html 。
-O
-O 参数将服务器回应保存成文件,并将 URL 的最后部分当作文件名。
$ curl -O https://www.example.com/foo/bar.html
上面命令将服务器回应保存成文件,文件名为 bar.html 。
-s
-s 参数将不输出错误和进度信息。
$ curl -s https://www.example.com
上面命令一旦发生错误,不会显示错误信息。不发生错误的话,会正常显示运行结果。
