Update curl.md

This commit is contained in:
小弟调调™ 2019-10-10 10:38:26 +08:00 committed by GitHub
parent 9054760206
commit 3f4a98277b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 164 additions and 17 deletions

View File

@ -134,16 +134,30 @@ curl URL --silent
使用选项`-O`将下载的数据写入到文件,必须使用文件的绝对地址:
```shell
curl http://wangchujiang.com/text.iso --silent -O
curl http://example.com/text.iso --silent -O
```
选项`-o`将下载数据写入到指定名称的文件中,并使用`--progress`显示进度条:
```shell
curl http://wangchujiang.com/test.iso -o filename.iso --progress
curl http://example.com/test.iso -o filename.iso --progress
######################################### 100.0%
```
**不输出错误和进度信息**
`-s` 参数将不输出错误和进度信息。
```shell
curl -s https://www.example.com
# 上面命令一旦发生错误,不会显示错误信息。不发生错误的话,会正常显示运行结果。
```
如果想让 curl 不产生任何输出,可以使用下面的命令。
```shell
curl -s -o /dev/null https://google.com
```
**断点续传**
curl能够从特定的文件偏移处继续下载它可以通过指定一个便宜量来下载部分文件
@ -165,20 +179,6 @@ curl -C -URL
curl --referer http://www.google.com http://wangchujiang.com
```
**用curl设置cookies**
使用`--cookie "COKKIES"`选项来指定cookie多个cookie使用分号分隔
```shell
curl http://wangchujiang.com --cookie "user=root;pass=123456"
```
将cookie另存为一个文件使用`--cookie-jar`选项:
```shell
curl URL --cookie-jar cookie_file
```
**用curl设置用户代理字符串**
有些网站访问会提示只能使用IE浏览器来访问这是因为这些网站设置了检查用户代理可以使用curl把用户代理设置为IE这样就可以访问了。使用`--user-agent`或者`-A`选项:
@ -212,6 +212,11 @@ curl URL --max-filesize bytes
如果文件大小超出限制命令则返回一个非0退出码如果命令正常则返回0。
```shell
curl --limit-rate 200k https://example.com
# 上面命令将带宽限制在每秒 200K 字节。
```
**用curl进行认证**
使用curl选项 -u 可以完成HTTP或者FTP的认证可以指定密码也可以不指定密码在后续操作中输入密码
@ -248,7 +253,26 @@ curl -v "http://www.wangchujiang.com" # 显示get请求全过程解析
**post请求**
```shell
curl -d "param1=value1&param2=value2" "http://www.wangchujiang.com"
$ curl -d "param1=value1&param2=value2" "http://www.wangchujiang.com/login"
curl -d'login=emmapassword=123' -X POST https://wangchujiang.com/login
# 或者
$ curl -d 'login=emma' -d 'password=123' -X POST https://wangchujiang.com/login
```
`--data-urlencode` 参数等同于 `-d`,发送 `POST` 请求的数据体,区别在于会自动将发送的数据进行 `URL` 编码。
```shell
curl --data-urlencode 'comment=hello world' https://wangchujiang.com/login
# 上面代码中发送的数据hello world之间有一个空格需要进行 URL 编码。
```
**读取本地文本文件的数据,向服务器发送**
```shell
curl -d '@data.txt' https://wangchujiang.com/upload
# 读取data.txt文件的内容作为数据体向服务器发送。
```
**json格式的post请求**
@ -257,6 +281,129 @@ curl -d "param1=value1&param2=value2" "http://www.wangchujiang.com"
curl -l -H "Content-type: application/json" -X POST -d '{"phone":"13521389587","password":"test"}' http://wangchujiang.com/apis/users.json
```
**向服务器发送 Cookie**
使用`--cookie "COKKIES"`选项来指定cookie多个cookie使用分号分隔
```shell
curl http://wangchujiang.com --cookie "user=root;pass=123456"
```
将cookie另存为一个文件使用`--cookie-jar`选项:
```shell
curl URL --cookie-jar cookie_file
```
`-b` 参数用来向服务器发送 Cookie。
```shell
curl -b 'foo=bar' https://taobao.com
# 上面命令会生成一个标头Cookie: foo=bar向服务器发送一个名为foo、值为bar的 Cookie。
```
```shell
curl -b 'foo1=bar' -b 'foo2=baz' https://taobao.com
# 上面命令发送两个 Cookie。
```shell
curl -b cookies.txt https://www.taobao.com
# 上面命令读取本地文件 cookies.txt里面是服务器设置的 Cookie参见-c参数将其发送到服务器。
```
**Cookie 写入一个文件**
```shell
curl -c cookies.txt https://www.taobao.com
# 上面命令将服务器的 HTTP 回应所设置 Cookie 写入文本文件cookies.txt。
```
**请求的来源**
`-e` 参数用来设置 `HTTP` 的标头 `Referer`,表示请求的来源。
```shell
curl -e 'https://taobao.com?q=example' https://www.example.com
# 上面命令将Referer标头设为 https://taobao.com?q=example。
```
`-H` 参数可以通过直接添加标头 `Referer`,达到同样效果。
```shell
curl -H 'Referer: https://taobao.com?q=example' https://www.example.com
```
**上传二进制文件**
`-F` 参数用来向服务器上传二进制文件。
```shell
curl -F 'file=@photo.png' https://taobao.com/profile
# 上面命令会给 HTTP 请求加上标头 Content-Type: multipart/form-data 然后将文件photo.png作为file字段上传。
```
`-F` 参数可以指定 `MIME` 类型。
```shell
curl -F 'file=@photo.png;type=image/png' https://taobao.com/profile
# 上面命令指定 MIME 类型为image/png否则 curl 会把 MIME 类型设为 application/octet-stream。
```
`-F` 参数也可以指定文件名。
```shell
curl -F 'file=@photo.png;filename=me.png' https://taobao.com/profile
# 上面命令中原始文件名为photo.png但是服务器接收到的文件名为me.png。
```
**设置请求头**
`-H` 参数添加 `HTTP` 请求的标头。
```shell
curl -H 'Accept-Language: en-US' https://google.com
# 上面命令添加 HTTP 标头 Accept-Language: en-US。
```
```shell
curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com
# 上面命令添加两个 HTTP 标头。
```
```shell
curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login
# 上面命令添加 HTTP 请求的标头是 Content-Type: application/json然后用 -d 参数发送 JSON 数据。
```
**跳过 SSL 检测**
```shell
curl -k https://www.example.com
# 上面命令不会检查服务器的 SSL 证书是否正确。
```
**请求跟随服务器的重定向**
`-L` 参数会让 `HTTP` 请求跟随服务器的重定向。`curl` 默认不跟随重定向。
```shell
curl -L -d 'tweet=hi' https://api.example.com/tweet
```
**调试参数**
`-v` 参数输出通信的整个过程,用于调试。
```shell
curl -v https://www.example.com
# --trace参数也可以用于调试还会输出原始的二进制数据。
```
```shell
$ curl --trace - https://www.example.com
```
**获取本机外网ip**
```shell