12 月 132017
 

Source: 使用curl指令測試REST服務

Table of contents


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兩個功能是一樣的,所以使用時U+00A0.ex:curl -X POST http://www.example.com/U+00A0.跟U+00A0.curl --request POST http://www.example.com/U+00A0.是相等的功能

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,也要加上引號

在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&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 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_fileU+00A0.的方式在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 上傳資料,U+00A0.-FU+00A0.是使用http query parameter的方式,指定檔案位置的參數要加上@

HTTP Basic Authentication (HTTP基本認證)

如果網站是採HTTP基本認證, 可以使用U+00A0.--user username:passwordU+00A0.登入

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

認證通過時,會回應U+00A0.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

相關資源

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

CAPTCHA Image
Play CAPTCHA Audio
Reload Image