博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux下curl命令的使用
阅读量:7080 次
发布时间:2019-06-28

本文共 3720 字,大约阅读时间需要 12 分钟。

hot3.png

1.curl

     简单来说,curl是一个用url方式,来和服务器进行文件传输和下载的工具。它不仅仅支持Http协议,还支持了其他的众多的协议,例如DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP等。

 

2.curl工具格式和重要的选项

格式:

[plain]

 

  1. curl [options] [URL...]  

重要选项:

[html]

 

  1. -X method, --request method       #指定使用http的method,有GET/POST/PUT/DELETE等等,默认是GET方法  
  2. -d key=value, --data key=value    #指定HTTP请求中请求数据段,例如HTTP POST请求时需要传递给服务器的数据,一个curl命令中,可以用多个-d选项,curl会将他们合并成key1=value1&key2=value2…  
  3. --data-urlencode key=value        #类似于-d key=value,但是会经过URL编码  
  4. -F key=value, --form key=value    #模拟向服务器提交表单form数据,curl会用multipart/form-data的格式传递给服务器,而-d选项使用的是applica-tion/x-www-form-urlencoded,一个crul命令中,同样可以有多个-F选项  
  5. -e url, --referer url             #在curl发起的http请求的请求头中,设置referer信息,向目标请求站点说明本次的http请求是来自那个http页面  
  6. -H <header>, --header <header>    #为http请求设置请求头信息,例如,--header "Content-Type:application/json" -H Host:www.baididu.com  
  7. -A <agent string>, --user-agent<agent string>   #为http请求设置user-agent信息,这个字段是用来表示客户端的设备信息。服务器有时会根据这个字段,针对不同设备,返回不同格式的网页  
  8. -u <user:password;options>, --user<user:password;options>   #为http请求设置用户名和密码  
  9. -o filename        #将curl返回的请求结果,写入到filename文件中  
  10. -b <namename=data>, --cookie<namename=data>, -b <cookie-file>, --cookie <cookie-file>  #为curl的http请求携带cookies信息,可以在命令行用key=value设置,也可以从一个文件中读取  
  11. -c <cookie-file>, --cookie-jar <cookie-file>      #将服务器返回的cookie信息写入到本地文件中  
  12. -v, --verbose          #显示curl的http请求的通信过程,直接打印到终端上  
  13. --trace <file>         #将curl的http请求通信过程写入到文件中  
  14. --trace-ascii <file>   #同--trace <file>类似,将curl的http请求通信过程写入到文件中  
  15. -I           #只输出HTTP响应报文的头部  
  16. -i           #输出HTTP响应报文的头部以及响应正文  
  17. -s           #让curl开启静默模式,即不输出进度或错误等信息  
  18. -L           #如果服务端返回3XX重定向,curl会继续向新地址发送请求  
  19. -x ip:port   #设置http请求的代理服务器,若端口不指定,默认为1080  

 

3.curl使用的例子

 

[html]

 

  1. curl http://www.example.com  
  2. curl -o sina.output http://www.example.com  
  3. curl -s -o /dev/null http://www.example.com  
  4. curl -v http://www.example.com  
  5. curl --trace output.txt http://www.example.com  
  6. curl http://example.com/form.cgi?data=xxx  
  7. curl -X POST --data "data=xxx" http://example.com/form.cgi  
  8. curl -X POST --data-urlencode "date=April 1" http://example.com/form.cgi  
  9. curl --referer http://www.example.com http://www.example.com  
  10. curl --form upload=@localfilename --form press=OK http://www.example.com  #利用curl上传文件  
  11. curl --cookie "name=xxx" www.example.com  
  12. curl --header "Content-Type:application/json" http://example.com  
  13. curl -X PUT -H "Accept: application/json" http://example.com/v1/user/add --basic -u user:passwd \  
  14.      -F proto_file=@task.proto                                          \  
  15.      -F message_name="adduser"                                          \  
  16.      -F host=hostname                                                   \  
  17.      -F part_count=4                                                    \  
  18.      -F replication=5                                                   \  
  19.      -F part_rule=MOD                                                   \  
  20.      -F cpu_num=20                                                      \  
  21.      -F mem_mb=10000                                                    \  
  22.      -F disk_mb=10000                                                   \  
  23.      -F token_pattern="token"                                           \  
  24. curl -I -H "Host:www.example.com" www.example.com  

 

备注:狭义地说,curl像是一个浏览器,但是比浏览器的支持的东西要多,因为它不仅支持Http协议,还支持其他很多的协议。

 

4.关于使用curl命令的-d参数携带HTTP request的请求体(request body)的一些问题

HTTP Requset请求头中的Content-Type是用来说明请求体的MIME类型的,默认是application/x-www-form-urlencoded类型。curl -d参数是用携带POST/PUT请求的请求体内容的,有如下几种支持的格式:
(1)

[plain]

 

  1. curl -d "param1=value1&param2=value2" -X POST http://localhost:3000/data  

备注:Content-Type缺省为application/x-www-form-urlencoded,所以使用param1=value1&param2=value2格式时,可省略。

(2)

[plain]

 

  1. curl -d "param1=value1&param2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/data  

备注:使用param1=value1&param2=value2格式时,也可以显式地指出application/x-www-form-urlencoded.

(3)

[plain]

 

  1. curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/data  

备注:使用json格式的数据,一定要显式地指明Content-Type为application/json.

(4)

[plain]

 

  1. curl -d "@data.txt" -X POST http://localhost:3000/data  

备注:将param1=value1&param2=value2格式的数据单独放入文件,然后通过-d "@filename"来引入

(5)

[plain]

 

  1. curl -d "@data.json" -H "Content-Type: application/json" -X POST http://localhost:3000/data  

备注:将json格式的数据单独放入文件,然后通过-d "@filename"来引入

转载于:https://my.oschina.net/u/588516/blog/1584489

你可能感兴趣的文章
favicon.ico
查看>>
mysql数据同步-基于二进制日志文件和position复制点的方式
查看>>
arm-linux-gcc 常用参数讲解 gcc编译器使用方法
查看>>
深度探索Linux操作系统:系统构建和原理解析
查看>>
大规模Web服务开发技术
查看>>
java的锁机制
查看>>
滚动数字Label
查看>>
Simulator Photo Importer
查看>>
在Windows环境下MongoDB搭建和简单操作
查看>>
Android系统Camera录像过程分析
查看>>
快速找出你机器中的“木马”
查看>>
CentOS安装Oracle JDK
查看>>
PAV OPENCART 商城自适应主题模板 ABC-0013
查看>>
Django 查询集 QuerySet API文档
查看>>
Java设计模式
查看>>
多线程,高并发,锁
查看>>
上线后数据库出现 too many connection问题
查看>>
python os模块实例(批量修改图片名称)
查看>>
Flask中实现统一异常处理
查看>>
ECharts官方配置项解析(1)
查看>>