用CURL命令行測試REST API 無疑是低效率的,這里把最近使用的兩款 Chrome 插件總結下
- POSTMAN
- 簡單易用
- REST Console
- 功能強大
使用的話用POSTMAN就夠用了,但是我更喜歡 REST Console ,因為她的功能非常強大和全面,一下子就能讓你搞清楚你在做的事情,你用不到的功能也可以幫助你更加了解 REST, http請求的過程。
下面是兩個的截圖界面
- POSTMAN 測試工具
1. Authorization
- Basic Auth
- Digest Auth
- OAuth 1.0
2. REQUEST METHOD
GET, PUT, POST, PATCH, DELETE, LINK, UNLINK, COPY, HEAD, OPTIONS, PURGE
3. Request Headers
Content-Type:
- form-data
- x-www-form-urlencoded
- raw
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryVQuD0ghtRqDehBQH
Request Payload:
------WebKitFormBoundaryVQuD0ghtRqDehBQH
Content-Disposition: form-data; name="image"
dddddd
------WebKitFormBoundaryVQuD0ghtRqDehBQH--
Content-Type:application/x-www-form-urlencoded
Form Data:
image:bbbbbbb
Content-Type:text/plain;charset=UTF-8
Request Payload:
image=aaaaaaaaaaaaaa
這3種方法其中 form-data 是不支持 PUT 方法的。而用REST Console中的 Content-Type:multpart/form-data 是支持 PUT 方法的。不知道是不是bug
4. Response Body
支持3種展示方式, 以及常用的XML和JSON格式。
- Pretty
- Raw
- Preview
- JSON/XML
5. Response Header
Connection →
Connection
Options that are desired for the connection
Keep-Alive
Content-Length →93
Content-Type →application/json; charset=UTF-8
Date →Fri, 01 Aug 2014 05:41:56 GMT
Keep-Alive →timeout=5, max=100
Server →Apache/2.2.9 (Win32) PHP/5.4.30 mod_fcgid/2.3.6
X-Powered-By →PHP/5.4.30
- REST Console 測試工具
1. Options
軟件相關設置,配色,主題,高亮設置等(說明這個東東功能比較全面)
特別說下一個選項就是
Help Lines
, 開啟這個選項,對着 REST Console的每一個選項,就很容易搞清楚 http 的 請求和響應中的每一個項目是怎么回事
原創文章,轉載請注明 : http://www.cnblogs.com/ganiks/
2. Target
設置下面內容:
- Target
- Request URI : 這個是請求的 URI
- Request Method : PUT POST ...
- Request Timeout
- Accept
- Accept: (注意區分這個type和后面的content-type)
*/*
(一般都是這個選項)- application/atom+xml
- test/plain
- application/javascript
- application/json
- application/http
- application/pdf
- application/rar
- ... ...
- Acceptable Language
- Accept: (注意區分這個type和后面的content-type)
3. Body
- Content Headers
- Content-Type: mime type of the request body(跟PUT和POST方法配合使用)
- application/x-www-form-urlencoded
- text/plain
- multipart/form-data
- 這3種是在 POSTMAN中支持的3種,但其實有很多很多種,在 REST Console 中的輸入框中輸入幾個字母,會自動匹配庫中的很多選項
- Acceptable Encoding: 比如 utf-8(參考 HTTP compression)
- Content-MD5: 比如 Q2hlY2sgSW50ZWdyaXR5IQ==
- Content-Type: mime type of the request body(跟PUT和POST方法配合使用)
- Request Payload
- RAW BODY: image=ccccccccc
- Request Params: key=>value
- Attachements: upload files
- Custom Headers
- Request Parameters
對照個例子:
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4
Authorization:Basic MTAwLXRva2VuOg==
Cache-Control:no-cache
Connection:keep-alive
Content-Length:20
Content-Type:text/plain;charset=UTF-8
Host:192.168.4.126
Origin:chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
4. Authorization
- Basic Auth
- Setup oAuth
- Refresh oAuth
5. Headers
- Headers
- Max-Forwards: 10 (限制消息可以穿破 的代理和網關的層數)
- Range
- From: user@example.com (發送消息者的郵件地址)
- Warning
- Pragma
- ...
- Cache (內容太多了)
- Common non-standard request headers
6. Response
- Response Body: 支持 JSON XML HTML CSS 等高亮格式
{
"id": "162",
"image": "cccccccc",
"link": "dd2",
"show_date": "0000-00-00",
"state": 1,
"show_order": 0
}
- RAW Body
{"id":"162","image":"cccccccc","link":"dd2","show_date":"0000-00-00","state":1,"show_order":0}
- Response Headers
Status Code: 200
Date: Fri, 01 Aug 2014 06:39:00 GMT
Server: Apache/2.2.9 (Win32) PHP/5.4.30 mod_fcgid/2.3.6
Connection: Keep-Alive
X-Powered-By: PHP/5.4.30
Content-Length: 94
Keep-Alive: timeout=5, max=100
Content-Type: application/json; charset=UTF-8
- Response Preview
- Request Body
Request Url: http://192.168.4.126/news/162
Request Method: PUT
Status Code: 200
Params: {}
- Request Headers
Content-Type: multipart/form-data
Authorization: Basic MTAwLXRva2VuOg==
Accept: */*
Connection: keep-alive
Origin: chrome-extension: //rest-console-id
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
- http請求的Accept參數
===
上面提到了一個設置的地方, Accept
和 Content-Type
這兩個參數很重要
前者一般如果不設置默認的是 */*
, POSTMAN 和 RESTConsole 工具中默認的是 application/json
,因此在不設置 Headers
:Accept
參數的情況下返回的按照 json格式;
而普通的瀏覽器中返回的則默認是 application/xml
格式。
后者這個 type 指的是head body 內容的 類型
這就是為什么這兩個參數分別被 REST Console 工具分別放在了 2. Target
3. Body
中。
在 yii 中默認支持的 rest api 格式有 xml 和 JSON, yii 會根據 請求的 head 的 Accept 參數來返回對應格式的數據。
這個參數 在chrome 中可以修改默認值嗎?