|
3 h0 N& ^ x. S
cURL是什么?c 可以看作是 client,url(Uniform Resource Locator)是统一资源定位符cURL 可以为指定的 url 执行网络传输,在 shell 和脚本中它是非常便捷、强大、可靠的。
0 ]3 u9 B5 d, l; ~ cURL 支持 N 多协议(ftp、smtp等等),本文只讨论有关 http 基于命令行的相关话题,使用 cURL 完全可以轻而易举地取代 postman 之流的图形界面工具下面看下使用 cURL 发起
% p+ j& G# w/ C http 请求使用 cURL 发起 http 请求发起 http get 请求curl http://localhost:8080/demo使用-v 详细显示请求响应相关信息curl -v http:。
6 T- e; B( Y [. L: d( x; X9 `' { //localhost:8080/demo使用-G -d 发起get请求并发送数据curl -G -d "hello" -v http://1ocalhost:8080/demo使用-I 发起head请求 + e7 n( K* C. n1 T
curl -I http://localhost:8080/demo使用-i 响应包含头部信息curl -i http://localhost:8080/demo以上是基本的get请求示例,下面看下使用curl发起需要登录认证的请求。 4 {; ^, M6 Q9 {
使用 cURL 发起需要登录认证的请求使用-u 提供用户名密码curl -u admin:admin http://localhost:9002/actuatorcurl自动识别用户名密码curl http://admin:admin
" A8 H- V3 q3 _" X @localhost:9002/actuator
% ^, r) j' W9 [ 1使用-u 仅输入用户名 会提示密码输入curl -u admin http://localhost:9002/actuator使用-c 保存服务端响应的cookie ( m) g, J) R! n; f
curl -u admin:admin -c cookie.txt http://localhost:9002/actuator使用-b 携带cookie信息发起http请求curl -b cookie.txt http: $ i, }7 p" P5 P( h% l& ^
//localhost:9002/actuator下面看下使用curl发送post请求使用 cURL 发送 post 请求使用-d 发送http post请求数据 -H指定head line头信息curl -d 。 # K" l5 {5 c( g
"{name:star,age:20}" -H "Content-type:application/json" http://localhost:8080/demo/post使用@引用文件 包含请求数据的文件
3 A; o' }2 ^) r4 H curl-d@post_data -H "Content-type:application/json"http://localhost:8080/demo/post使用-F选项 post上传文件curl 1 }) e3 P5 K6 _* m+ ]) ~- T
-F fileName=@curl.pnghttp://localhost:8080/demo/file使用–data-urlencode编码 提交数据curl --data-urlencode name=码农小麦 & v2 \8 f; g" ^) E' p( B
-v http://localhost:8080/demo/urlencode使用-d 提交请求数据curl -d name=码农小麦 -d content=欢迎来撩 -v http://localhost:8080/demo/post' y" q9 |# Q+ w2 K" W; L( z
curl -d
( C( c2 L: N' z8 B: O/ n- x7 N' i name=码农小麦&content=欢迎来撩 -v http://localhost:8080/demo/post
' Q8 ~: x0 t" x, s8 A* [ 1以上就是 cURL 常见的命令行使用示例,完全可以应对日常的开发测试场景,以及脚本相关 http 请求功能实现。
1 h+ q/ W3 g, j 更多使用方法参见 curl --help & q$ z i2 W/ Q- F7 ]
* Q/ k. }. ?) H N7 `$ t: a5 n. X$ X) k; [4 o3 H, n. \, ~) b) e
' h' D. P k3 b9 V1 n2 f. K
' v/ {) l# n- H. `0 x+ b% A |