|
8 w7 O' y# n5 ?/ X# y' k1 g( k 作者:Yungacurl 常用总结curl是一个利用URL语法在命令行模式下工作的文件传输工具,是我们经常用到的工具,用得最多的便是用来测试http接口在这里对部分请求的使用方式做一个总结,熟练使用curl后,在不能使用Postman等图形工具的时候,也可以方便的调试自己的接口。
7 B1 s. b9 I0 }! g+ W Hello curlcurl http://test.com/这便是发起一个最简单http请求,不带任何请求参数GETcurl http://test.com/test?param1=a¶ms2=b。
1 L, N$ r% C' h ~( C2 S' v url传参,多个参数使用 & 连接自定义headercurl http://test.com/test -H TEST_HEADER1: hello -H TEST_HEADER2: world使用 -H . M. U$ i* `; _, [
来传递http请求的header,格式:key:value,如果需要多个header,使用多个 -H 即可POSTcurl可以以5种方式设置POST的内容:--data 通用设置--data-ascii 将对数据进行ASCII编码。 0 C$ j% J# ]; M+ H
--data-binary 直接将传递的数据转为二进制--data-raw 可以上传任意格式的文本,可以上传text、json、xml、html等--data-urlencode 将参数进行url编码例: 1 F& v. Y7 x5 ~, ? T7 N% B3 ~
## application/x-www-form-urlencoded 以键值对方式POST传递参数,curl默认方式- ~, R1 W2 P4 ^* z9 f+ K3 E
curl http://test.com/test -d param1=hello¶m2=curl
( H# Y1 F5 [0 S0 s- m& V( g7 d2 ^ Z1 c) C9 j
## 传递JSON字符串
% v, ]6 M2 M( r; f ~ ?2 Z curl http://test.com/test --data-raw my name is yunga -H Content-Type: application/json
; \0 ~! F' [. b) H: O! R
& R8 t4 X0 P+ \. t ## 数据需要进行url编码
% {3 r- G7 S0 Z- t- ` curl http://test.com/test --data-urlencode key=a+a+a+ 6 s+ k0 ?0 r D9 `
根据需要设置不同POST内容传递方式,设置对应的header设置Methodcurl -X PUT http://test.com/test -d param1=hello¶m2=curl可以使用这种方式设置需要的请求方式:GET、POST、PUT、DELETE、OPTION。 0 A/ ^" W' g# L$ |1 X: o! j2 T* p& R
上传文件curl -X POST http://test.com/test -F file=@hello.txt -F "key=what"使用这种方式,Content-Type会被设置为multipart/form-data,通过表单的形式提交文件,需要注意的是文件路径前使用 % s- G o8 N+ C$ \, b- n3 _
@ 来表示这是一个文件路径,否则会作为普通字符串传递,传递文件的同时我们可以传递其他的参数,使用多个 -F 即可忽略https证书验证curl -k -X POST http://test.com/test -F file=@hello.txt -F "key=what"。 . M# }6 u$ z6 R3 o( ~, o& |/ J+ F' B$ |
使用 -k 选项,使用SSL时允许不安全的服务器连接设置http协议版本使用以下选项来设置请求时http协议版本版本--http0.9--http1.0--http1.1--http2例:curl -X DELETE http://test.com/test -d key=a+a+a+ --http1.0。
; b: M+ O- K6 v+ `2 B( ` s% f( p 设置cookie## 直接设置cookie2 P: E0 k6 S" k; [- I5 P0 e# L
curl http://test.com/test --cookie "user=admin;pass=admin"6 M2 _, P# _+ L, |2 { L/ {
' v. y. Y0 X1 b; H* M/ ?
## 保存cookie到文件
6 A% H2 x$ M8 @5 E, X( b# D curl -b cookie.txt http://test.com/test
% }* J+ \/ Q+ ]" U/ c% e' Y. f& ~! L8 K' c& Y1 R! J6 G
## 使用cookie文件
# ?: h+ K% \9 O: y0 x$ P/ p9 G curl -c @cookie.txt http://test.com/test
0 v. y1 \5 a" \! V5 e. d, N 打印响应和请求头很多时候我们需要查看响应和请求的头,只需要带上 -v 选项即可例:curl http://test.com/test -d key=a+a+a+ --http1.0 --cookie user=admin;password=root -v
+ \; A0 k# T2 p" k ``. r9 `# z5 M: @% O# z& E
% m6 H% E. h4 [
打印大概如下:& T1 _( h9 K& G: ?$ k
```txt8 d8 F A9 e# v0 u! I: U5 w6 G
* Trying ::1...
$ h) B7 g! \2 ?0 w( w% G6 s; o * TCP_NODELAY set
# r4 W* Z8 G. B4 \* ~ * Connected to localhost (::1) port 10086 (#0)
' @0 Q& u) ]' q% f% U1 O0 H- i > POST /test?hello=aaa HTTP/1.0
4 D; M: V+ h5 T7 T0 K > Host: localhost:10086
3 ?* R a, l) D9 `# k7 |) ^ > User-Agent: curl/7.64.11 q5 c2 }$ w3 E" f/ [1 T
> Accept: */*
+ {; `( }" X3 p4 a- Y > Cookie: user=admin;password=root
* @5 L& ]+ g% m. b: G% V# t > Content-Length: 10; L' T, D& Z& c
> Content-Type: application/x-www-form-urlencoded8 }9 R. ^8 M! w; O$ M8 V
>
( p8 X8 z4 `+ b% b% k6 a) G$ @ * upload completely sent off: 10 out of 10 bytes$ B; N& D% U4 I
* HTTP 1.0, assume close after body
) @; n" l r K# }0 R < HTTP/1.0 200 OK1 @+ }' c* u/ M3 L3 W
< Date: Tue, 30 Jun 2020 12:12:48 GMT* L$ K3 S% P/ h/ V3 \; n, l' t4 \
< Content-Length: 7
# a% j& g& \7 T6 m < Content-Type: text/plain; charset=utf-8
. k" f( ?6 D# C P. v: @- ~, E <6 O6 j7 n1 n& s3 T( f& X
* Closing connection 0
: m, F) e% q8 o9 {: P success。 7 P' }4 r3 H: i6 K4 N. I/ `
& @! w, x) P& _! b# N& L) T
+ ^# Z' R% h# W/ A/ g
6 i# j4 M$ u( M3 j7 _
1 O; W' n' c* g) A+ H# s3 v4 V- K |