|
2 ?' T5 _+ X+ d ^: v
cURL是什么?c 可以看作是 client,url(Uniform Resource Locator)是统一资源定位符cURL 可以为指定的 url 执行网络传输,在 shell 和脚本中它是非常便捷、强大、可靠的。
! z# { m; M; {; `" F) D cURL 支持 N 多协议(ftp、smtp等等),本文只讨论有关 http 基于命令行的相关话题,使用 cURL 完全可以轻而易举地取代 postman 之流的图形界面工具下面看下使用 cURL 发起 1 D: U) g+ n# U
http 请求使用 cURL 发起 http 请求发起 http get 请求curl http://localhost:8080/demo使用-v 详细显示请求响应相关信息curl -v http:。 5 m' a) G" ~4 ^- l W6 U+ o; h
//localhost:8080/demo使用-G -d 发起get请求并发送数据curl -G -d "hello" -v http://1ocalhost:8080/demo使用-I 发起head请求
2 I1 u1 o$ o* Q' u& y$ ~& r+ ~6 W9 H curl -I http://localhost:8080/demo使用-i 响应包含头部信息curl -i http://localhost:8080/demo以上是基本的get请求示例,下面看下使用curl发起需要登录认证的请求。
) N& l2 f' g# `4 ^& T! X3 L 使用 cURL 发起需要登录认证的请求使用-u 提供用户名密码curl -u admin:admin http://localhost:9002/actuatorcurl自动识别用户名密码curl http://admin:admin
; A% Y* x, t- M+ q3 ` @localhost:9002/actuator
3 s1 e9 l: w4 t$ w 1使用-u 仅输入用户名 会提示密码输入curl -u admin http://localhost:9002/actuator使用-c 保存服务端响应的cookie . g" r( {6 E, s: ]8 o
curl -u admin:admin -c cookie.txt http://localhost:9002/actuator使用-b 携带cookie信息发起http请求curl -b cookie.txt http: 4 B& q5 ^7 R6 h) z
//localhost:9002/actuator下面看下使用curl发送post请求使用 cURL 发送 post 请求使用-d 发送http post请求数据 -H指定head line头信息curl -d 。 7 q A6 ~2 i! v8 B' V- q1 l
"{name:star,age:20}" -H "Content-type:application/json" http://localhost:8080/demo/post使用@引用文件 包含请求数据的文件
5 d* S4 c: g i# | curl-d@post_data -H "Content-type:application/json"http://localhost:8080/demo/post使用-F选项 post上传文件curl ( ~, `4 S' q7 l
-F fileName=@curl.pnghttp://localhost:8080/demo/file使用–data-urlencode编码 提交数据curl --data-urlencode name=码农小麦 " ^6 j5 c7 {( y- G' c( q9 D
-v http://localhost:8080/demo/urlencode使用-d 提交请求数据curl -d name=码农小麦 -d content=欢迎来撩 -v http://localhost:8080/demo/post& a( n4 J7 P3 y/ v$ G' L$ v) h
curl -d 5 c3 T0 @. L0 L: t( u2 j; e' T
name=码农小麦&content=欢迎来撩 -v http://localhost:8080/demo/post5 ]1 x f" x1 K! P7 O. G- n3 {! ]
1以上就是 cURL 常见的命令行使用示例,完全可以应对日常的开发测试场景,以及脚本相关 http 请求功能实现。 # d5 H( x' b7 L2 @( `' E) d
更多使用方法参见 curl --help 8 G d' D+ T3 \' }( g) y/ ^
4 L7 N! W* U. g1 v. [0 J3 U6 T% O, C2 h
8 K' q% \/ ~9 [, T3 L U7 K3 F* ^" _
|