cURL 是很方便的Rest客护端,可以很方便的完成许多Rest API测试的需求,甚至,如果是需要先登入或认证的rest api,也可以进行测试,利用curl指令,可以送出HTTP GET, POST, PUT, DELETE, 也可以改变 HTTP header来满足使用REST API需要的特定条件。
curl的参数很多,这边仅列出目前测试REST时常用到的:
-X/--request [GET|POST|PUT|DELETE|…] 使用指定的http method发出 http request
-H/--header 设定request裡的header
-i/--include 显示response的header
-d/--data 设定 http parameters
-v/--verbose 输出比较多的讯息
-u/--user 使用者帐号、密码
-b/--cookie cookie
linux command line 的参数行,同一个功能常会有两个功能完全相同参数,一个是比较短的参数,前面通常是用-(一个-)导引符号,另一个比较长的参数,通常会用–(两个-)导引符号
在curl 使用说明
-X, --request COMMAND Specify request command to use
--resolve HOST:PORT:ADDRESS Force resolve of HOST:PORT to ADDRESS
--retry NUM Retry request NUM times if transient problems occur
--retry-delay SECONDS When retrying, wait this many seconds between each
--retry-max-time SECONDS Retry only within this period>
参数-X跟–request两个功能是一洋的,所以使用时 ex:curl -X POST http://www.example.com/ 跟 curl –request POST http://www.example.com/ 是相等的功能
GET/POST/PUT/DELETE使用方式
-X 后面加 http method,
curl -X GET "http://www.rest.com/api/users"
curl -X POST "http://www.rest.com/api/users"
curl -X PUT "http://www.rest.com/api/users"
curl -X DELETE "http://www.rest.com/api/users"
url要加引号也可以,不加引号也可以,如果有非纯英文字或数字外的字元,不加引号可能会有问题,如果是网码过的url,也要加上引号
HEADER
在http header加入的讯息
curl -v -i -H "Content-Type: application/json" http://www.example.com/users
HTTP Parameter
http参数可以直接加在url的query string,也可以用-d带入参数间用&串接,或使用多个-d
# 使用`&`串接多个惨数
curl -X POST -d "param1=value1¶m2=value2"
# 也可使用多个`-d`,效果同上
curl -X POST -d "param1=value1" -d "param2=value2"
curl -X POST -d "param1=a 0space"
# "a space" url encode后空白字元会编码成'%20'为"a%20space",编码后的参数可以直接使用
curl -X POST -d "param1=a%20space"
post json 格式
如同时需要传送request parameter跟json,request parameter可以加在url后面,json资料则放入-d的参数,然后利用单引号将json资料含起来(如果json内容是用单引号,-d的参数则改用双引号包覆),header要加入”Content-Type:application/json”跟”Accept:application/json”
curl http://www.example.com?modifier=kent -X PUT -i -H "Content-Type:application/json" -H
"Accept:application/json" -d '{"boolean" : false, "foo" : "bar"}'
# 不加"Accept:application/json"也可以
curl http://www.example.com?modifier=kent -X PUT -i -H "Content-Type:application/json"
-d '{"boolean" : false, "foo" : "bar"}'
需先认证或登入才能使用的service
许多服务,需先进行登入或认证后,才能存取其API服务,依服务要求的条件,的curl可以透过cookie,session或加入在header加入session key,api key或认证的token来达到认证的效果。
session 例子:
后端如果是用session记录使用者登入资讯,后端会传一个 session id给前端,前端需要在每次跟后端的requests的header中置入此session id,后端便会以此session id识别前端是属于那个session,以达到session的效果
curl --request GET 'http://www.rest.com/api/users' --header 'sessionid:1234567890987654321'
cookie 例子
如果是使用cookie,在认证后,后端会回一个cookie回来,把该cookie成档案,当要存取需要任务的url时,再用-b cookie_file 的方式在request中植入cookie即可正常使用
# 将cookie存档
curl -i -X POST -d username=kent -d password=kent123 -c ~/cookie.txt http://www.rest.com/auth
# 载入cookie到request中
curl -i --header "Accept:application/json" -X GET -b ~/cookie.txt http://www.rest.com/users/1
档案上传
curl -i -X POST -F 'file=@/Users/kent/my_file.txt' -F 'name=a_file_name'
这个是透过 HTTP multipart POST 上传资料, -F 是使用http query parameter的方式,指定档案位置的惨数要加上@
HTTP Basic Authentication (HTTP基本认证)
如果网站是採HTTP基本认证, 可以使用 –user username:password 登入
curl -i --user kent:secret http://www.rest.com/api/foo'
认证失败时,会是401 Unauthorized
HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
WWW-Authenticate: Basic realm="Realm"
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 1022
Date: Thu, 15 May 2014 06:32:49 GMT
认证通过时,会回应 200 OK
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Set-Cookie: JSESSIONID=A75066DCC816CE31D8F69255DEB6C30B; Path=/mdserver/; HttpOnly
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 15 May 2014 06:14:11 GMT
可以把认证后的cookie存起来,重複使用
curl -i --user kent:secret http://www.rest.com/api/foo' -c ~/cookies.txt
登入之前暂存的cookies,可以不用每次都认证
curl -i http://www.rest.com/api/foo' -b ~/cookies.txt