curl 是罕用的命令行工具,用来申请 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。
它的性能十分弱小,命令行参数多达几十种。如果纯熟的话,齐全能够取代 Postman 这一类的图形界面工具。
本文介绍它的次要命令行参数,作为日常的参考,不便查阅。内容次要翻译自《curl cookbook》。为了节约篇幅,上面的例子不包含运行时的输入,初学者能够先看我以前写的《curl 初学者教程》。
不带有任何参数时,curl 就是收回 GET 申请。
$ curl https://www.example.com
下面命令向www.example.com收回 GET 申请,服务器返回的内容会在命令行输入。
-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
下面命令将User-Agent改成 Chrome 浏览器。
$ curl -A '' https://google.com
下面命令会移除User-Agent标头。
也能够通过-H参数间接指定标头,更改User-Agent。
$ curl -H 'User-Agent: php/1.0' https://google.com
-b
-b参数用来向服务器发送 Cookie。
$ curl -b 'foo=bar' https://google.com
下面命令会生成一个标头Cookie: foo=bar,向服务器发送一个名为foo、值为bar的 Cookie。
$ curl -b 'foo1=bar;foo2=bar2' https://google.com
下面命令发送两个 Cookie。
$ curl -b cookies.txt https://www.google.com
下面命令读取本地文件cookies.txt,外面是服务器设置的 Cookie(参见-c参数),将其发送到服务器。
-c
-c参数将服务器设置的 Cookie 写入一个文件。
$ curl -c cookies.txt https://www.google.com
下面命令将服务器的 HTTP 回应所设置 Cookie 写入文本文件cookies.txt。
-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 编码。
-e
-e参数用来设置 HTTP 的标头Referer,示意申请的起源。
$ curl -e 'https://google.com?q=example' https://www.example.com
下面命令将Referer标头设为https://google.com?q=example。
-H参数能够通过间接增加标头Referer,达到同样成果。
curl -H 'Referer: https://google.com?q=example' https://www.example.com
-F
-F参数用来向服务器上传二进制文件。
$ curl -F 'file=@photo.png' https://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=k...。如果省略--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
-k
-k参数指定跳过 SSL 检测。
$ curl -k https://www.example.com
下面命令不会查看服务器的 SSL 证书是否正确。
-L
-L参数会让 HTTP 申请追随服务器的重定向。curl 默认不追随重定向。
$ curl -L -d 'tweet=hi' https://api.twitter.com/tweet
--limit-rate
--limit-rate用来限度 HTTP 申请和回应的带宽,模仿慢网速的环境。
$ curl --limit-rate 200k https://google.com
下面命令将带宽限度在每秒 200K 字节。
-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 -s -o /dev/null https://google.com
下面命令没有任何输入,除非产生谬误。
-u
-u参数用来设置服务器认证的用户名和明码。
$ curl -u 'bob:12345' https://google.com/login
下面命令设置用户名为bob,明码为12345,而后将其转为 HTTP 标头Authorization: Basic Ym9iOjEyMzQ1。
curl 可能辨认 URL 外面的用户名和明码。
$ curl https://bob:12345@google.com/login
下面命令可能辨认 URL 外面的用户名和明码,将其转为上个例子外面的 HTTP 标头。
$ curl -u 'bob' https://google.com/login
下面命令只设置了用户名,执行后,curl 会提醒用户输出明码。
-v
-v参数输入通信的整个过程,用于调试。
$ curl -v https://www.example.com
--trace参数也能够用于调试,还会输入原始的二进制数据。
$ curl --trace - https://www.example.com
-x
-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
-X参数指定 HTTP 申请的办法。
$ curl -X POST https://www.example.com
以上就是良许教程网为各位朋友分享的如何curl 的用法。想要理解更多Linux相干常识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你!
以上就是良许教程网为各位朋友分享的Linux相干常识。