curl 命令详解(超详细)

GET 请求

GET 方法是在 curl 中发出请求的默认方法,因此不必指定任何参数。
eg: curl https://blog.ucwords.com

-o 保存响应到文件中
curl -o response.tex https://blog.ucwords.com

POST 请求

简单请求
curl -X POST https://blog.ucwords.com

Form Data
curl -d 'name=123&age=23' -x POST https://blog.ucwords.com curl -d 'name=123' -d 'age=23' https://blog.ucwords.com

当使用参数 -d ,curl自动携带请求头:Content-Type:multipart/form-data。
当使用参数 -d ,-X POST 可以省略,因为会隐式发起 POST 请求。

JSON Data
curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://blog.ucwords.com
需要指定请求头: ‘Content-Type: application/json’。

–data-urlencode 将参数 URL 编码方式
如下参数间有空格会被编码为 %20
curl --data-urlencode 'name=1212 few' https://blog.ucwords.com

如果需要携带的参数没有经过 URL 编码,需要使用 --data-urlencode 进行处理,数据在通过网络发送出去之前被curl进行了URL编码。
需要指定请求头: ‘Content-Type: application/x-www-form-urlencoded’。

上传文件
curl -d '@data.txt' https://blog.ucwords.com
@是标志文件而非简单字符串。

Multipart Data 或者上传二进制文件
用 -F 参数,强制 curl 发出多表单数据的 POST 请求,自动携带 -H “Content-Type: multipart/form-data”。当需要上传图像或其他二进制文件时,发送多表单数据非常有用。
curl -F 'file=@image.png' https://blog.ucwords.com

上传二进制文件并设置其 MIME 类型
curl -F 'file=@image.png;type:image/png' https://blog.ucwords.com
curl -F 'file=@image.png;type:image/png;filename=target.png' https://blog.ucwords.com
如果没有指定类型,curl 会将其设置为 application/octet-stream。

请求头

-H 设置请求头
curl -H 'Content-Type: application/json' -H 'Accept-Language: en-US' https://blog.ucwords.com

user-agent

-A 设置 UA
curl -A 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0' https://blog.ucwords.com
清空则设置 -A “”

cookie

-b 设置 cookie
curl -b 'session=33232' -b 'session2=123fdreq' https://blog.ucwords.com
* 默认情况下,curl 不发送任何 cookie

-c 保存 cookie
curl -c cookie.txt https://blog.ucwords.com

从文件加载 cookie
curl -b cookie.txt https://blog.ucwords.com

Referrer

-e 添加 referrer
curl -e 'https://google.com' https://blog.ucwords.com
清空则设置 -e “”

-H 也可以设置到 header中。
curl -H 'Referer: https://google.com' https://blog.ucwords.com

Redirect 重定向

curl -L https://blog.ucwords.com
使 curl 始终遵循 301、302、303 或任何其他 3XX 重定向。默认情况下,curl 不遵循重定向。

Basic Auth

-u user:pass
curl -u 'zhang:124' https://blog.ucwords.com

也可以放到URL中
curl https://user:pass@baidu.com

提示输入密码
curl -u 'user' https://blog.ucwords.com

Print Response 打印响应

同时打印响应头和正文
收到响应时,首先打印标题,然后是空白链接,然后是响应。
curl -i https://blog.ucwords.com

仅打印响应头
curl 中几乎不可能只打印响应标头(并忽略响应正文),所以需要使用多个参数间接得到。
curl -s -o /dev/null -D - https://blog.ucwords.com

代理设置

Socks5 代理
curl -x socks5://james:cats@myproxy.com:8080 https://blog.ucwords.com

Socks4 代理
curl -x socks4://james:cats@myproxy.com:8080 https://blog.ucwords.com

HTTP 代理
curl -x james:cats@myproxy.com:8080 https://blog.ucwords.com

不要为 xxx.com 使用代理
curl --no-proxy xxx.com https://blog.ucwords.com https://xxx.com

忽略 SSL 证书

curl -k https://blog.ucwords.com
指定证书版本 curl (-1-2-3) 代表 SSLv1、SSLv2、 SSLv3

静默输出

使用 -s 不会打印进度条、错误和其他可能妨碍的输出。要同时隐藏响应,请使用-o /dev/null(-o NUL 在 Windows 上)。

隐藏错误和进度条(但打印响应)
curl -s https://blog.ucwords.com

全部静默
curl -s -o /dev/null https://blog.ucwords.com

仅输出错误(当有)
curl -S -s -o /dev/null https://blog.ucwords.com
更多见 curl 调试部分。

限制 curl 带宽

使用 --limit-rate speed 选项 (k、m、g等单位)。
1. 限制每秒 20 KB :
curl --limit-rate 20k https://blog.ucwords.com
2. 限制每秒 1 字节:
curl --limit-rate 1 https://blog.ucwords.com

curl 调试

-v 参数打印有关请求和响应的详细信息。
curl -v https://blog.ucwords.com
输出结果解析:
1. 前缀以 > 开头的行是发送给服务器的数据。
2. 前缀以 > 开头的行是从服务器接收的数据.
3. 前缀以 * 开头的行如连接信息、SSL 握手信息、协议信息等。

–trace - 参数用来启用所有传入和传出数据的完整跟踪转储。跟踪转储打印发送和接收的所有字节的 hexdump。
curl --trace - https://blog.ucwords.com

–trace-time 带有时间戳的详细 trace
curl --trace - --trace-time https://blog.ucwords.com

仅打印请求头
curl -v -s -o /dev/null --stderr - https://blog.ucwords.com | grep '^>'

仅打印响应代码
curl -w '%{response_code}' -s -o /dev/null https://blog.ucwords.com

其他参数

-G 将参数以 & 链接拼接到 URL 后面
curl -G -d 'a=1' -d 'b=20' https://blog.ucwords.com

-I --head 发起 HEAD 请求
curl -I https://blog.ucwords.com

-o -O 保存响应到文件
curl -o response.txt https://blog.ucwords.com

-X 指定 HTTP 请求的方法
curl -X POST https://blog.ucwords.com

  • 17
    点赞
  • 136
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Windows下的curl命令是一个非常强大的网络工具,它可以用来发送HTTP请求、下载文件、上传文件等。以下是curl命令的详细介绍: 1. 发送HTTP请求 curl命令可以用来发送HTTP请求,例如: curl http://www.example.com 这个命令会向www.example.com发送一个GET请求,并输出服务器返回的内容。 2. 下载文件 curl命令还可以用来下载文件,例如: curl -O http://www.example.com/file.zip 这个命令会下载www.example.com上的file.zip文件,并保存到当前目录下。 3. 上传文件 curl命令还可以用来上传文件,例如: curl -F "file=@/path/to/file" http://www.example.com/upload 这个命令会将/path/to/file文件上传到www.example.com的/upload接口。 4. 设置请求头 curl命令可以设置请求头,例如: curl -H "Content-Type: application/json" http://www.example.com/api 这个命令会向www.example.com的/api接口发送一个Content-Type为application/json的请求。 5. 设置请求方法 curl命令可以设置请求方法,例如: curl -X POST http://www.example.com/api 这个命令会向www.example.com的/api接口发送一个POST请求。 6. 设置请求参数 curl命令可以设置请求参数,例如: curl -d "name=John&age=30" http://www.example.com/api 这个命令会向www.example.com的/api接口发送一个name为John、age为30的POST请求。 以上就是Windows下curl命令的详细介绍。需要注意的是,curl命令的参数非常多,可以根据具体需求进行调整。 ### 回答2: Windows curl命令是一个在Windows平台下的命令行工具,它通过HTTP、FTP、SMTP和其他协议与服务器进行通信,实现简单的数据传输操作,例如下载和上传文件,获取网页内容和发送电子邮件等。 Windows curl命令具有丰富的选项参数,提供了多种功能和配置选项,可以支持发送各种HTTP请求、设置请求头、发送表单数据、支持文件传输等多种功能。 1.基本使用方法 curl命令的基本语法如下: ```bash curl [options] [URL...] ``` 其中,options为curl命令的选项参数,URL为操作的目标地址。例如,使用curl命令获取百度首页的内容: ```bash curl https://www.baidu.com ``` 2. 选项参数详解 - -A:发送HTTP请求时设置User-Agent头; - -d:发送POST请求时提交的表单数据; - -F:上传文件时使用; - -H:发送HTTP请求时添加的额外头信息; - -I:仅显示头信息; - -o:将URL返回的内容输出到文件; - -s:不显示进度和错误信息; - -v:显示详细的操作过程信息。 3. 文件传输 发送文件和接收文件是curl很重要的功能。curl采用FTP传输协议,支持文件的上传和下载。 - 下载文件: ```bash curl -O url ``` 其中,-O选项表示将URL文件的名称直接下载到本地。 - 上传文件: ```bash curl -T file url ``` 其中,-T选项表示发送本地文件到URL指定的位置。 4. 接口测试 通过curl可以测试Api接口。下面是一个使用curl测试API接口的例子: ```bash curl -X POST -H "Content-Type: application/json" -d '{"name": "Tom", "age": 18}' http://api.example.com/v1/persons/ ``` 其中,-X选项表示发送POST请求,-H选项为设置请求头中的Content-Type,-d选项表示提交的表单数据。 综上所述,Windows curl命令是一种非常方便的命令行工具,能够支持多种协议和功能。使用它可以轻松地完成文件传输、接口测试等功能。对文件下载、上传、API接口测试等操作的小型软件开发作业非常实用。 ### 回答3: curl是一个常用的命令行工具,用于在网络上发送数据,支持多种协议,如HTTP、FTP、SMTP等。Windows curl命令是curl在Windows操作系统上的版本,它可以完成与curl命令在其他操作系统上相同的功能,比如下载文件、发送数据、模拟请求等。 Windows curl命令的基本语法是: ```powershell curl [options] [URL...] ``` 其中,URL是指要请求的网址,可以为单个网址或多个网址,多个URL之间用空格分隔。 Windows curl命令常用的选项包括: 1. `-o` 或 `--output`: 指定输出的文件名或路径。 ```powershell curl -o file.txt http://example.com/file.txt ``` 2. `-O` 或 `--remote-name`: 根据URL中指定的文件名,将文件保存到当前目录下。 ```powershell curl -O http://example.com/file.txt ``` 3. `-L` 或 `--location`: 自动跟随重定向。 ```powershell curl -L http://example.com ``` 4. `-u` 或 `--user`: 指定用户名和密码。 ```powershell curl -u username:password http://example.com ``` 5. `-d` 或 `--data`: 发送POST请求的数据。 ```powershell curl -d "name=value" http://example.com ``` 6. `-H` 或 `--header`: 添加HTTP请求头。 ```powershell curl -H "Content-Type: application/json" http://example.com ``` 7. `-X` 或 `--request`: 指定HTTP请求方法。 ```powershell curl -X POST http://example.com ``` 8. `-i` 或 `--include`: 输出完整的HTTP头。 ```powershell curl -i http://example.com ``` 以上只是Windows curl命令中一部分选项的介绍,还有许多其他的选项可以根据需求进行调整。总体而言,Windows curl命令是一个十分强大的网络工具,能够为网络开发和调试提供便利。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mooddance

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值