一目了然
目標: 從截獲的 API 請求建立一個 HTTP 檔案以便重複使用。
時間: 10分鐘
插件:HttpFileGeneratorPlugin
前置條件:設定開發代理
這個開發 Proxy 能夠從被攔截的 API 請求和回應中生成 HTTP 檔案。 使用 HTTP 檔案特別適用於想要模擬 API 行為或共用可重現 API 互動的開發人員。 HTTP 檔案包含所有相關的要求和回應詳細數據,並以變數取代的敏感性資訊,以取得安全性和重複使用性。
若要使用 Dev Proxy 產生 HTTP 檔案:
在組態檔中, 啟用
HttpFileGeneratorPlugin:檔案: devproxyrc.json
{ "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.0.0/rc.schema.json", "plugins": [ { "name": "HttpFileGeneratorPlugin", "enabled": true, "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll", "configSection": "httpFileGeneratorPlugin" } ], "urlsToWatch": [ "https://api.example.com/*" ], "httpFileGeneratorPlugin": { "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.0.0/httpfilegeneratorplugin.schema.json", "includeOptionsRequests": false } }選擇將
includeOptionsRequests屬性加入httpFileGeneratorPlugin區段,以設定該外掛。 此特性決定是否在產生的 HTTP 檔案中包含OPTIONS請求。 預設值為false。在設定檔中,到要監控的 URL 清單中,加入你想產生 HTTP 檔案的 API URL。
完整的設定檔長這樣。
檔案: devproxyrc.json
{ "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.0.0/rc.schema.json", "plugins": [ { "name": "HttpFileGeneratorPlugin", "enabled": true, "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll", "configSection": "httpFileGeneratorPlugin" } ], "urlsToWatch": [ "https://api.example.com/*" ], "httpFileGeneratorPlugin": { "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.0.0/httpfilegeneratorplugin.schema.json", "includeOptionsRequests": false } }啟動開發代理伺服器:
devproxy按下
r來開始錄製要求。執行您想要包含在 HTTP 檔案中的 API 要求。
按
s停止錄製。開發 Proxy 會產生 HTTP 檔案,並將它儲存在當前目錄中。 此檔案包含所有擷取的要求和回應,其中包含由變數取代的敏感數據,例如持有人令牌和 API 金鑰。 例如:
@jsonplaceholder_typicode_com_api_key = api-key ### # @name getPosts GET https://jsonplaceholder.typicode.com/posts?api-key={{jsonplaceholder_typicode_com_api_key}} Host: jsonplaceholder.typicode.com User-Agent: curl/8.6.0 Accept: */* Via: 1.1 dev-proxy/0.29.0外掛程式會自動為每個主機名和敏感性參數組合建立變數,並在適用時跨要求重複使用它們。
後續步驟
深入瞭解 HttpFileGeneratorPlugin。
另請參閱
- HttpFileGeneratorPlugin - 完整參考
- 記錄與匯出代理活動 - 記錄工作流程
- 詞彙表 - 開發代理術語