Curl用法:修订间差异

来自MediaWiki
跳转到导航 跳转到搜索
Admin留言 | 贡献
导入1个版本
 
Admin留言 | 贡献
页面内容被替换为“<p>https://curl.se/docs/manual.html</p>”
第1行: 第1行:
<p>curl -H "Content-Type: application/json" -X POST -d '{"user_id": "123", "coin":100, "success":1, "msg":"OK!" }' "<a href="http://192.168.0.1:8001/test" target="_blank">http://192.168.0.1:8001/test</a>"</p>
<p>https://curl.se/docs/manual.html</p>
<p>参数 内容<br>
-H 请求头<br>
-d POST内容<br>
-X 请求协议</p>
<p>简介<br>
curl 是常用的命令行工具,用来请求 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。它的功能非常强大,命令行参数多达几十种。如果熟练的话,完全可以取代 Postman 这一类的图形界面工具。</p>
<h2 id="-a"><strong>-A</strong></h2>
<p><code>-A</code>参数指定客户端的用户代理标头,即<code>User-Agent</code>。curl 的默认用户代理字符串是<code>curl/[version]</code>。</p>
<blockquote>
<pre><code class="language-bash">$ 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
</code></pre>
</blockquote>
<p>上面命令将<code>User-Agent</code>改成 Chrome 浏览器。</p>
<blockquote>
<pre><code class="language-bash">$ curl -A '' https://google.com
</code></pre>
</blockquote>
<p>上面命令会移除<code>User-Agent</code>标头。</p>
<p>也可以通过<code>-H</code>参数直接指定标头,更改<code>User-Agent</code>。</p>
<blockquote>
<pre><code class="language-bash">$ curl -H 'User-Agent: php/1.0' https://google.com
</code></pre>
</blockquote>
<h2 id="-b"><strong>-b</strong></h2>
<p><code>-b</code>参数用来向服务器发送 Cookie。</p>
<blockquote>
<pre><code class="language-bash">$ curl -b 'foo=bar' https://google.com
</code></pre>
</blockquote>
<p>上面命令会生成一个标头<code>Cookie: foo=bar</code>,向服务器发送一个名为<code>foo</code>、值为<code>bar</code>的 Cookie。</p>
<blockquote>
<pre><code class="language-bash">$ curl -b 'foo1=bar;foo2=bar2' https://google.com
</code></pre>
</blockquote>
<p>上面命令发送两个 Cookie。</p>
<blockquote>
<pre><code class="language-bash">$ curl -b cookies.txt https://www.google.com
</code></pre>
</blockquote>
<p>上面命令读取本地文件<code>cookies.txt</code>,里面是服务器设置的 Cookie(参见<code>-c</code>参数),将其发送到服务器。</p>
<h2 id="-c"><strong>-c</strong></h2>
<p><code>-c</code>参数将服务器设置的 Cookie 写入一个文件。</p>
<blockquote>
<pre><code class="language-bash">$ curl -c cookies.txt https://www.google.com
</code></pre>
</blockquote>
<p>上面命令将服务器的 HTTP 回应所设置 Cookie 写入文本文件<code>cookies.txt</code>。</p>
<h2 id="-d"><strong>-d</strong></h2>
<p><code>-d</code>参数用于发送 POST 请求的数据体。</p>
<blockquote>
<pre><code class="language-bash">$ 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
</code></pre>
</blockquote>
<p>使用<code>-d</code>参数以后,HTTP 请求会自动加上标头<code>Content-Type : application/x-www-form-urlencoded</code>。并且会自动将请求转为 POST 方法,因此可以省略<code>-X POST</code>。</p>
<p><code>-d</code>参数可以读取本地文本文件的数据,向服务器发送。</p>
<blockquote>
<pre><code class="language-bash">$ curl -d '@data.txt' https://google.com/login
</code></pre>
</blockquote>
<p>上面命令读取<code>data.txt</code>文件的内容,作为数据体向服务器发送。</p>
<h2 id="--data-urlencode"><strong>--data-urlencode</strong></h2>
<p><code>--data-urlencode</code>参数等同于<code>-d</code>,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码。</p>
<blockquote>
<pre><code class="language-bash">$ curl --data-urlencode 'comment=hello world' https://google.com/login
</code></pre>
</blockquote>
<p>上面代码中,发送的数据<code>hello world</code>之间有一个空格,需要进行 URL 编码。</p>
<h2 id="-e"><strong>-e</strong></h2>
<p><code>-e</code>参数用来设置 HTTP 的标头<code>Referer</code>,表示请求的来源。</p>
<blockquote>
<pre><code class="language-bash">curl -e 'https://google.com?q=example' https://www.example.com
</code></pre>
</blockquote>
<p>上面命令将<code>Referer</code>标头设为<code>https://google.com?q=example</code>。</p>
<p><code>-H</code>参数可以通过直接添加标头<code>Referer</code>,达到同样效果。</p>
<blockquote>
<pre><code class="language-bash">curl -H 'Referer: https://google.com?q=example' https://www.example.com
</code></pre>
</blockquote>
<h2 id="-f"><strong>-F</strong></h2>
<p><code>-F</code>参数用来向服务器上传二进制文件。</p>
<blockquote>
<pre><code class="language-bash">$ curl -F 'file=@photo.png' https://google.com/profile
</code></pre>
</blockquote>
<p>上面命令会给 HTTP 请求加上标头<code>Content-Type: multipart/form-data</code>,然后将文件<code>photo.png</code>作为<code>file</code>字段上传。</p>
<p><code>-F</code>参数可以指定 MIME 类型。</p>
<blockquote>
<pre><code class="language-bash">$ curl -F 'file=@photo.png;type=image/png' https://google.com/profile
</code></pre>
</blockquote>
<p>上面命令指定 MIME 类型为<code>image/png</code>,否则 curl 会把 MIME 类型设为<code>application/octet-stream</code>。</p>
<p><code>-F</code>参数也可以指定文件名。</p>
<blockquote>
<pre><code class="language-bash">$ curl -F 'file=@photo.png;filename=me.png' https://google.com/profile
</code></pre>
</blockquote>
<p>上面命令中,原始文件名为<code>photo.png</code>,但是服务器接收到的文件名为<code>me.png</code>。</p>
<h2 id="-g"><strong>-G</strong></h2>
<p><code>-G</code>参数用来构造 URL 的查询字符串。</p>
<blockquote>
<pre><code class="language-bash">$ curl -G -d 'q=kitties' -d 'count=20' https://google.com/search
</code></pre>
</blockquote>
<p>上面命令会发出一个 GET 请求,实际请求的 URL 为<code>https://google.com/search?q=kitties&amp;count=20</code>。如果省略<code>--G</code>,会发出一个 POST 请求。</p>
<p>如果数据需要 URL 编码,可以结合<code>--data--urlencode</code>参数。</p>
<blockquote>
<pre><code class="language-bash">$ curl -G --data-urlencode 'comment=hello world' https://www.example.com
</code></pre>
</blockquote>
<h2 id="-h"><strong>-H</strong></h2>
<p><code>-H</code>参数添加 HTTP 请求的标头。</p>
<blockquote>
<pre><code class="language-bash">$ curl -H 'Accept-Language: en-US' https://google.com
</code></pre>
</blockquote>
<p>上面命令添加 HTTP 标头<code>Accept-Language: en-US</code>。</p>
<blockquote>
<pre><code class="language-bash">$ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com
</code></pre>
</blockquote>
<p>上面命令添加两个 HTTP 标头。</p>
<blockquote>
<pre><code class="language-bash">$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login
</code></pre>
</blockquote>
<p>上面命令添加 HTTP 请求的标头是<code>Content-Type: application/json</code>,然后用<code>-d</code>参数发送 JSON 数据。</p>
<h2 id="-i"><strong>-i</strong></h2>
<p><code>-i</code>参数打印出服务器回应的 HTTP 标头。</p>
<blockquote>
<pre><code class="language-bash">$ curl -i https://www.example.com
</code></pre>
</blockquote>
<p>上面命令收到服务器回应后,先输出服务器回应的标头,然后空一行,再输出网页的源码。</p>
<h2 id="-i-1"><strong>-I</strong></h2>
<p><code>-I</code>参数向服务器发出 HEAD 请求,然会将服务器返回的 HTTP 标头打印出来。</p>
<blockquote>
<pre><code class="language-bash">$ curl -I https://www.example.com
</code></pre>
</blockquote>
<p>上面命令输出服务器对 HEAD 请求的回应。</p>
<p><code>--head</code>参数等同于<code>-I</code>。</p>
<blockquote>
<pre><code class="language-bash">$ curl --head https://www.example.com
</code></pre>
</blockquote>
<h2 id="-k"><strong>-k</strong></h2>
<p><code>-k</code>参数指定跳过 SSL 检测。</p>
<blockquote>
<pre><code class="language-bash">$ curl -k https://www.example.com
</code></pre>
</blockquote>
<p>上面命令不会检查服务器的 SSL 证书是否正确。</p>
<h2 id="-l"><strong>-L</strong></h2>
<p><code>-L</code>参数会让 HTTP 请求跟随服务器的重定向。curl 默认不跟随重定向。</p>
<blockquote>
<pre><code class="language-bash">$ curl -L -d 'tweet=hi' https://api.twitter.com/tweet
</code></pre>
</blockquote>
<h2 id="--limit-rate"><strong>--limit-rate</strong></h2>
<p><code>--limit-rate</code>用来限制 HTTP 请求和回应的带宽,模拟慢网速的环境。</p>
<blockquote>
<pre><code class="language-bash">$ curl --limit-rate 200k https://google.com
</code></pre>
</blockquote>
<p>上面命令将带宽限制在每秒 200K 字节。</p>
<h2 id="-o"><strong>-o</strong></h2>
<p><code>-o</code>参数将服务器的回应保存成文件,等同于<code>wget</code>命令。</p>
<blockquote>
<pre><code class="language-bash">$ curl -o example.html https://www.example.com
</code></pre>
</blockquote>
<p>上面命令将<code>www.example.com</code>保存成<code>example.html</code>。</p>
<h2 id="-o-1"><strong>-O</strong></h2>
<p><code>-O</code>参数将服务器回应保存成文件,并将 URL 的最后部分当作文件名。</p>
<blockquote>
<pre><code class="language-bash">$ curl -O https://www.example.com/foo/bar.html
</code></pre>
</blockquote>
<p>上面命令将服务器回应保存成文件,文件名为<code>bar.html</code>。</p>
<h2 id="-s"><strong>-s</strong></h2>
<p><code>-s</code>参数将不输出错误和进度信息。</p>
<blockquote>
<pre><code class="language-bash">$ curl -s https://www.example.com
</code></pre>
</blockquote>
<p>上面命令一旦发生错误,不会显示错误信息。不发生错误的话,会正常显示运行结果。</p>
<p>如果想让 curl 不产生任何输出,可以使用下面的命令。</p>
<blockquote>
<pre><code class="language-bash">$ curl -s -o /dev/null https://google.com
</code></pre>
</blockquote>
<h2 id="-s-1"><strong>-S</strong></h2>
<p><code>-S</code>参数指定只输出错误信息,通常与<code>-s</code>一起使用。</p>
<blockquote>
<pre><code class="language-bash">$ curl -s -o /dev/null https://google.com
</code></pre>
</blockquote>
<p>上面命令没有任何输出,除非发生错误。</p>
<h2 id="-u"><strong>-u</strong></h2>
<p><code>-u</code>参数用来设置服务器认证的用户名和密码。</p>
<blockquote>
<pre><code class="language-bash">$ curl -u 'bob:12345' https://google.com/login
</code></pre>
</blockquote>
<p>上面命令设置用户名为<code>bob</code>,密码为<code>12345</code>,然后将其转为 HTTP 标头<code>Authorization: Basic Ym9iOjEyMzQ1</code>。</p>
<p>curl 能够识别 URL 里面的用户名和密码。</p>
<blockquote>
<pre><code class="language-bash">$ curl https://bob:12345@google.com/login
</code></pre>
</blockquote>
<p>上面命令能够识别 URL 里面的用户名和密码,将其转为上个例子里面的 HTTP 标头。</p>
<blockquote>
<pre><code class="language-bash">$ curl -u 'bob' https://google.com/login
</code></pre>
</blockquote>
<p>上面命令只设置了用户名,执行后,curl 会提示用户输入密码。</p>
<h2 id="-v"><strong>-v</strong></h2>
<p><code>-v</code>参数输出通信的整个过程,用于调试。</p>
<blockquote>
<pre><code class="language-bash">$ curl -v https://www.example.com
</code></pre>
</blockquote>
<p><code>--trace</code>参数也可以用于调试,还会输出原始的二进制数据。</p>
<blockquote>
<pre><code class="language-bash">$ curl --trace - https://www.example.com
</code></pre>
</blockquote>
<h2 id="-x"><strong>-x</strong></h2>
<p><code>-x</code>参数指定 HTTP 请求的代理。</p>
<blockquote>
<pre><code class="language-bash">$ curl -x socks5://james:cats@myproxy.com:8080 https://www.example.com
</code></pre>
</blockquote>
<p>上面命令指定 HTTP 请求通过<code>myproxy.com:8080</code>的 socks5 代理发出。</p>
<p>如果没有指定代理协议,默认为 HTTP。</p>
<blockquote>
<pre><code class="language-bash">$ curl -x james:cats@myproxy.com:8080 https://www.example.com
</code></pre>
</blockquote>
<p>上面命令中,请求的代理使用 HTTP 协议。</p>
<h2 id="-x-1"><strong>-X</strong></h2>
<p><code>-X</code>参数指定 HTTP 请求的方法。</p>
<blockquote>
<pre><code class="language-bash">$ curl -X POST https://www.example.com
</code></pre>
</blockquote>
<p>上面命令对<code>https://www.example.com</code>发出 POST 请求。</p>

2023年6月29日 (四) 08:58的版本