东行天下

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
统计
 

一、简介

curl 是常用的命令行工具,用来请求 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。
它的功能非常强大,命令行参数多达几十种。如果熟练的话,完全可以取代 Postman 这一类的图形界面工具。

二、语法

复制代码
如果在一个curl命令中不指定具体的方法,那么默认的就是使用GET方法。对于其它的方法,可以在curl命令中指定:
method option
POST -d或-F
HEAD -I

PUT

-T








curl [option] [url]

常见选项:
复制代码
 1 -A/--user-agent <string>             设置用户代理发送给服务器
 2 -b/--cookie <name=string/file>         cookie字符串或文件读取位置
 3 -c/--cookie-jar <file>               操作结束后把cookie写入到这个文件中
 4 -C/--continue-at <offset>            断点续转
 5 -D/--dump-header <file>              把header信息写入到该文件中
 6 -e/--referer                         来源网址
 7 -f/--fail                            连接失败时不显示http错误
 8 -o/--output                          把输出写到该文件中
 9 -O/--remote-name                     把输出写到该文件中,保留远程文件的文件名
10 -r/--range <range>                   检索来自HTTP/1.1或FTP服务器字节范围
11 -s/--silent                          静音模式。不输出任何东西
12 -T/--upload-file <file>              上传文件
13 -u/--user <user[:password]>          设置服务器的用户和密码
14 -w/--write-out [format]              什么输出完成后
15 -x/--proxy <host[:port]>             在给定的端口上使用HTTP代理
16 -#/--progress-bar                    进度条显示当前的传送状态
复制代码

curl支持的协议:
1 cURL支持的通信协议有:
2 FTP、FTPS、HTTP、HTTPS、TFTP、SFTP、Gopher、SCP、Telnet、DICT、FILE、LDAP、LDAPS、IMAP、POP3、SMTP和RTSP。
3 
4 curl还支持SSL认证、HTTP POST、HTTP PUT、FTP上传, 
5 
6 HTTP form based upload、proxies、HTTP/2、cookies、用户名+密码认证(Basic, Plain, Digest, CRAM-MD5, NTLM, 
7 
8 Negotiate and Kerberos)、file transfer resume、proxy tunneling。
复制代码

三、使用示例

1.保存网页

1 curl URL >> filename.html 或 curl -o filename.html URL

2.保存网页中的文件

curl -O http://www.baidu.com/hello.sh

3.测试网页响应码(返回值)

curl -o /dev/null -s -w %{http_code} www.baidu.com

4.显示下载进度条

1 curl -# -O https://scpic.chinaz.net/files/pic/pic9/202205/apic40779.jpg

5.跳过ssl检测

curl -k https://www.example.com

6.静默输出

curl -s https://www.example.com  # 不输出错误和进度信息,会输出结果
curl -s -o /dev/null https://google.com # 不输出任何信息
curl -S -o /dev/null https://google.com # 只输出错误信息

7.用户名及密码认证

curl -u 'bob:12345' https://google.com/login

8.调试

1 curl --trace - https://www.example.com # 调试及输出二进制数据
2 curl -v https://www.example.com          # 调试

9.指定http的请求方法

curl -X POST https://www.example.com

10.HTTP 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
 4 使用-d参数以后,HTTP 请求会自动加上标头Content-Type : application/x-www-form-urlencoded。并且会自动将请求转为 POST 方法,因此可以省略-X POST
 5 curl http://192.168.56.103:7788/demo/curl-test/post -X POST -H "Content-Type:application/json" -d '{"name": "leizi","age": 25,"address": "杭州"}'
 6 
 7 -d参数可以读取本地文本文件的数据,向服务器发送。
 8 curl -d '@data.txt' https://google.com/login
 9 
10 --data-urlencode参数等同于-d,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码。
11 curl --data-urlencode 'comment=hello world' https://google.com/login
复制代码

11.Content-Type

1 当使用POST方法提交数据时,对于提交的数据主要有如下四种形式:
2 
3 application/x-www-form-urlencoded:默认的形式,即key1=value1&key2=value2的形式;
4 multipart/form-data:使用表单上传文件时使用这个形式;
5 application/json:提交JSON格式的数据;
6 text/xml:提交XML格式的数据。

12.多个请求头 option: -H

1 curl http://192.168.56.103:7788/demo/curl-test/post -X POST -H "Content-Type:application/json" -H "token:6666" -d '{"name": "leizi","age": 25,"address": "杭州"}'

 

posted on   东行天下  阅读(111)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
历史上的今天:
2022-04-11 Mysql-主从复制
 
点击右上角即可分享
微信分享提示