什么是Interceptor
Postman Interceptor是可用于Chrome的擴展程序,可以捕獲和操縱Postman和Web服務(wù)器之間傳遞的HTTP請求。
Postman Interceptor可以直接從Chrome捕獲網(wǎng)絡(luò)請求,并將其保存到Postman的歷史記錄中。這意味著您可以實時調(diào)試您的網(wǎng)絡(luò)應(yīng)用程序API!沒有必要安裝或配置代理,并且沒有任何代碼需要更改。
您還可以使用正則表達式根據(jù)URL進行過濾請求。如果您有一個Web應(yīng)用程序但沒有建立集合,或者您只想調(diào)試應(yīng)用程序正在使用的API,使用它將會節(jié)省大量時間。
Postman Interceptor在Chrome平臺上構(gòu)建,該功能可以在Windows,Linux,Mac和Chrome操作系統(tǒng)上輕松實現(xiàn)。默認情況下,它還可以捕獲和操縱Cookie或設(shè)置那些在Chrome平臺上被阻止的某些HTTP header。
安裝Postman Interceptor
以下告訴您如何開始:
- 如果您還沒有安裝Postmand的話,請從Chrome網(wǎng)上應(yīng)用商店安裝。
- 安裝 Interceptor擴展。
- 打開Postman,然后單擊工具欄中的Interceptor圖標將開關(guān)切換到“開”。
- 瀏覽您的應(yīng)用程序或您的網(wǎng)站,并查看請求流入。
關(guān)于安全性的說明: Interceptor通信的唯一實體是Postman,然后將其保存到您的歷史記錄中。我們有開源的Interceptor,你可以在Github上找到代碼 。Postman將您所有的數(shù)據(jù)本地保存在IndexedDB中。
捕獲cookies
與Postman本地應(yīng)用程序不同,Postman Chrome應(yīng)用程序本身沒有能力處理Cookie。您可以使用Interceptor擴展來解決這個問題。使用Interceptor,您可以檢索特定域名中設(shè)置的Cookie,并在發(fā)送請求時提交Cookie。
檢索cookies
確保在Postman工具欄中啟用了Interceptor。
在測試(Tests)選項卡下,您可以使用“responseCookies”對象。這將返回一個cookie對象數(shù)組。要檢索特定的名稱,請使用“postman.getResponseCookie(cookieName)”。這將返回一個cookie對象。每個Cookie對象將包含以下屬性:domain,hostOnly,httpOnly,name,path,secure,session,storeId,value。
設(shè)置cookies
- 確保Interceptor已啟用。
- 在header部分包括“Cookie”頭(例如:Cookie:name = value; name2 = value2)。
- 發(fā)送請求。您設(shè)置的Cookie將隨您的請求一起發(fā)送到Chrome。
受限的header
不幸的是,某些header受Chrome和XMLHttpRequest規(guī)范限制。以下header被阻止:
- Accept-Charset
- Accept-Encoding
- Access-Control-Request-Headers
- Access-Control-Request-Method
- Connection
- Content-Length
- Cookie
- Cookie 2
- Content-Transfer-Encoding
- Date
- Expect
- Host
- Keep-Alive
- Origin
- Referer
- TE
- Trailer
- Transfer-Encoding
- Upgrade
- User-Agent
- Via
但是發(fā)送這些受限header很容易。請按照以下步驟操作:
- 通過點擊Postman工具欄或Chrome網(wǎng)上應(yīng)用店中的Interceptor圖標來安裝Interceptor擴展 。
- 安裝完成后,再次點擊Postman應(yīng)用中的Interceptor圖標并將其切換到開啟狀態(tài)。
大功告成!您現(xiàn)在可以發(fā)送使用這些headers的請求了。

