命令向www.example.com发出 GET 请求,服务器返回的内容会在命令行输出。

curl https://www.example.com
http://www.ruanyifeng.com/blog/2019/09/curl-reference.html

-A

-A 参数指定客户端的用户代理标头,即User-Agent。curl 的默认用户代理字符串是curl/[version]。
命令将User-Agent改成 Chrome 浏览器。

  1. 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
  2. curl -A '' https://google.com # 命令会移除User-Agent标头

也可以通过-H参数直接指定标头,更改User-Agent。

  1. curl -H 'User-Agent: php/1.0' https://google.com

-b

-b参数用来向服务器发送 Cookie。

  1. curl -b 'foo=bar' https://google.com
  2. curl -b 'foo1=bar;foo2=bar2' https://google.com
  3. curl -b cookies.txt https://www.google.com # 命令读取本地文件cookies.txt,里面是服务器设置的 Cookie(参见-c参数),将其发送到服务器。

-c

-c 参数将服务器设置的 Cookie 写入一个文件。

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

-d

-d参数用于发送 POST 请求的数据体。

  1. curl -d'login=emma&password=123'-X POST https://google.com/login
  2. # 或者
  3. 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参数可以读取本地文本文件的数据,向服务器发送。

  1. curl -d '@data.txt' https://google.com/login
  2. # 命令读取data.txt文件的内容,作为数据体向服务器发送。

—data-urlencode

—data-urlencode参数等同于-d,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码。

  1. $ curl --data-urlencode 'comment=hello world' https://google.com/login

上面代码中,发送的数据hello world之间有一个空格,需要进行 URL 编码。

-F

-F参数用来向服务器上传二进制文件。
$ curl -F ‘file=@photo.pnghttps://google.com/profile
上面命令会给 HTTP 请求加上标头Content-Type: multipart/form-data,然后将文件photo.png作为file字段上传。
-F参数可以指定 MIME 类型。
$ curl -F ‘file=@photo.png;type=image/png’ https://google.com/profile
上面命令指定 MIME 类型为image/png,否则 curl 会把 MIME 类型设为application/octet-stream。
-F参数也可以指定文件名。
$ curl -F ‘file=@photo.png;filename=me.png’ https://google.com/profile
上面命令中,原始文件名为photo.png,但是服务器接收到的文件名为me.png。

-G

-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

-H参数添加 HTTP 请求的标头。
$ curl -H ‘Accept-Language: en-US’ https://google.com
上面命令添加 HTTP 标头Accept-Language: en-US。
$ curl -H ‘Accept-Language: en-US’ -H ‘Secret-Message: xyzzy’ https://google.com
上面命令添加两个 HTTP 标头。
$ 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
上面命令收到服务器回应后,先输出服务器回应的标头,然后空一行,再输出网页的源码。

-I

-I参数向服务器发出 HEAD 请求,然会将服务器返回的 HTTP 标头打印出来。
$ curl -I https://www.example.com
上面命令输出服务器对 HEAD 请求的回应。
—head参数等同于-I。
$ curl —head https://www.example.com

-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
上面命令一旦发生错误,不会显示错误信息。不发生错误的话,会正常显示运行结果。
如果想让 curl 不产生任何输出,可以使用下面的命令。
$ curl -s -o /dev/null https://google.com

-S

-S参数指定只输出错误信息,通常与-s一起使用。
$ curl -s -o /dev/null https://google.com
上面命令没有任何输出,除非发生错误。