使用curl指令测试REST服务

2017-06-10  本文已影响706人  木猫尾巴

[TOC]

说明

测试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/UNIX 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两个功能是一样的

所以使用时
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

curl -v -i -H "Content-Type: application/json" http://www.example.com/users
# 只输出头参数,适合用于调试
curl -v -I http://www.example.com/users
curl -v -I -H "Content-Type: application/json" http://www.example.com/users

HTTP Parameter

# 使用`&`串接多个参数
curl -X POST -d "param1=value1&param2=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 put json 格式

如同时需要传送request parameter跟json

curl http://www.example.com?modifier=kent -X POST-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"}'

登录需要认证的服务器

session 例子

curl --request GET 'http://www.rest.com/api/users' --header 'sessionid:1234567890987654321'

cookie 例子

# 将cookie存档
curl -i -X POST -d username=kent -d password=kent123 -c  ~/cookie.txt  http://www.rest.com/auth
# 载入cookie到request中

文件上传测试

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基本认证, 可以使用 --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
上一篇 下一篇

猜你喜欢

热点阅读