|
. \& @2 D! k4 Y$ I- a 作者:Yungacurl 常用总结curl是一个利用URL语法在命令行模式下工作的文件传输工具,是我们经常用到的工具,用得最多的便是用来测试http接口在这里对部分请求的使用方式做一个总结,熟练使用curl后,在不能使用Postman等图形工具的时候,也可以方便的调试自己的接口。
6 z3 x3 y2 R, ]% Z; M, B! z( w Hello curlcurl http://test.com/这便是发起一个最简单http请求,不带任何请求参数GETcurl http://test.com/test?param1=a¶ms2=b。
0 ~ D$ h3 A8 z- p% b' ?/ Y url传参,多个参数使用 & 连接自定义headercurl http://test.com/test -H TEST_HEADER1: hello -H TEST_HEADER2: world使用 -H
2 o7 Y# H6 i. x/ ?' ?! L 来传递http请求的header,格式:key:value,如果需要多个header,使用多个 -H 即可POSTcurl可以以5种方式设置POST的内容:--data 通用设置--data-ascii 将对数据进行ASCII编码。
0 X8 @, K* A* } --data-binary 直接将传递的数据转为二进制--data-raw 可以上传任意格式的文本,可以上传text、json、xml、html等--data-urlencode 将参数进行url编码例:
* I. E+ l8 z, P* m ## application/x-www-form-urlencoded 以键值对方式POST传递参数,curl默认方式5 T1 D1 V* D# R% g% g7 _
curl http://test.com/test -d param1=hello¶m2=curl1 g- ^) T- u7 t, x1 o
( d' Y, C/ B1 c$ {" l1 g: q5 x
## 传递JSON字符串
6 v4 ^% ^5 x' D' g curl http://test.com/test --data-raw my name is yunga -H Content-Type: application/json
# J- Z; o6 q" E. G3 I0 e! }( ^: {. \. t& c- n9 B: J
## 数据需要进行url编码! `( O! ~, b9 h; H' _
curl http://test.com/test --data-urlencode key=a+a+a+
' t. D- A5 D2 L* O9 C. C 根据需要设置不同POST内容传递方式,设置对应的header设置Methodcurl -X PUT http://test.com/test -d param1=hello¶m2=curl可以使用这种方式设置需要的请求方式:GET、POST、PUT、DELETE、OPTION。
9 h& u* O+ O0 H" N' P! R 上传文件curl -X POST http://test.com/test -F file=@hello.txt -F "key=what"使用这种方式,Content-Type会被设置为multipart/form-data,通过表单的形式提交文件,需要注意的是文件路径前使用 ' X- a2 J3 d6 B5 s7 e5 @( L0 y
@ 来表示这是一个文件路径,否则会作为普通字符串传递,传递文件的同时我们可以传递其他的参数,使用多个 -F 即可忽略https证书验证curl -k -X POST http://test.com/test -F file=@hello.txt -F "key=what"。 # K Z' O2 N- f0 @; i u
使用 -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。
8 ~6 \5 C: |2 t9 q 设置cookie## 直接设置cookie: r, S+ [1 f/ {9 M9 c; p! y
curl http://test.com/test --cookie "user=admin;pass=admin"
' E& ~$ r" E2 N; C$ B) }; a: P# ?( Z
## 保存cookie到文件5 d4 j# O9 e) D- H# t
curl -b cookie.txt http://test.com/test
% B$ ]1 v8 ~5 I9 S: |0 x3 |4 w3 r. ]! [. n. ?4 H
## 使用cookie文件
, x3 |$ p7 Q9 G" @1 a curl -c @cookie.txt http://test.com/test
+ a# q. t3 M& l! D- `2 W 打印响应和请求头很多时候我们需要查看响应和请求的头,只需要带上 -v 选项即可例:curl http://test.com/test -d key=a+a+a+ --http1.0 --cookie user=admin;password=root -v0 k( o3 c, a- V& U( U& y \
``7 C8 O7 m# l5 I0 ^5 D& K& _
, o$ z/ F! C7 z) e9 m) u4 ?
打印大概如下:
+ S% x- j7 W6 y! K- b9 G% r ```txt
6 t( c! S# q6 g; z+ n0 |" k4 S * Trying ::1...9 a( ], N. {4 t. n
* TCP_NODELAY set2 j" [4 w# B P% N
* Connected to localhost (::1) port 10086 (#0)
$ b5 U: l1 G! R > POST /test?hello=aaa HTTP/1.0) U& b* _* ^; w
> Host: localhost:10086
0 ?! s0 V& }. ?, x9 e- K > User-Agent: curl/7.64.1
* ~' c& O8 u7 R4 Z$ P > Accept: */*
: g$ N: i% S. S > Cookie: user=admin;password=root
$ a. L6 R3 i8 K1 i: `' b- @1 D > Content-Length: 107 ~0 V: o" y" b; d
> Content-Type: application/x-www-form-urlencoded
1 Y! @4 M4 n8 U5 I% g0 k# d# P >6 y: d. s2 _, `
* upload completely sent off: 10 out of 10 bytes* l- p( S* `, L; Y4 U( T) J
* HTTP 1.0, assume close after body
4 C9 Q- | j- _+ [ < HTTP/1.0 200 OK2 }7 X: u) ~: N; N
< Date: Tue, 30 Jun 2020 12:12:48 GMT
8 C# G6 M( ]# u4 r9 d$ U+ _ < Content-Length: 72 [3 z" Q8 P5 _( U' Z5 i/ t" w
< Content-Type: text/plain; charset=utf-8
( J' ~; a( Y: Z" b$ ?6 x. ?* _ <
; b+ l4 I6 Y3 X3 K7 E5 C( [ * Closing connection 0. i- a( }) Z9 |- l
success。 / o$ _1 ~& H/ S! h! l h) i) K
% Y0 k6 f: ?: K' z/ S# {# Q
1 |& g4 `7 Q& z* Q+ t9 r6 {
/ M) }. _0 @5 J" U* B& U8 r1 d
; I9 i, v1 G4 [# p |