HTTP「讀取、求值、輸出」迴圈 (REPL) 是:
- 在所有 .NET 支援的平臺上運行的輕量型跨平臺命令列工具。
- 用來提出 HTTP 要求來測試 ASP.NET Core Web API (及非 ASP.NET Core 的 Web API) 並檢視其結果。
- 能夠測試裝載於任何環境中的 Web API,包括 localhost 和 Azure App Service。
支援的 HTTP 動詞命令如下:
若要跟著做,請檢視或下載範例 ASP.NET Core web API (如何下載)。
Prerequisites
Installation
若要安裝 HttpRepl,請執行下列命令:
dotnet tool install -g Microsoft.dotnet-httprepl
.NET 全域工具會從 Microsoft.dotnet-httprepl NuGet 套件安裝。
Note
根據預設,要安裝的 .NET 二進位檔架構代表目前執行的 OS 架構。 若要指定不同的 OS 架構,請參閱 dotnet tool install, --arch option。 如需詳細資訊,請參閱 GitHub 問題 dotnet/AspNetCore.Docs #29262。
在 macOS 上更新路徑:
export PATH="$HOME/.dotnet/tools:$PATH"
Usage
成功安裝工具後,請執行以下命令來啟動 HttpRepl:
httprepl
若要檢視可用的 HttpRepl 命令,請執行以下其中一個命令:
httprepl -h
httprepl --help
下列輸出顯示如下:
Usage:
httprepl [<BASE_ADDRESS>] [options]
Arguments:
<BASE_ADDRESS> - The initial base address for the REPL.
Options:
-h|--help - Show help information.
Once the REPL starts, these commands are valid:
Setup Commands:
Use these commands to configure the tool for your API server
connect Configures the directory structure and base address of the api server
set header Sets or clears a header for all requests. e.g. `set header content-type application/json`
HTTP Commands:
Use these commands to execute requests against your application.
GET get - Issues a GET request
POST post - Issues a POST request
PUT put - Issues a PUT request
DELETE delete - Issues a DELETE request
PATCH patch - Issues a PATCH request
HEAD head - Issues a HEAD request
OPTIONS options - Issues a OPTIONS request
Navigation Commands:
The REPL allows you to navigate your URL space and focus on specific APIs that you are working on.
ls Show all endpoints for the current path
cd Append the given directory to the currently selected path, or move up a path when using `cd ..`
Shell Commands:
Use these commands to interact with the REPL shell.
clear Removes all text from the shell
echo [on/off] Turns request echoing on or off, show the request that was made when using request commands
exit Exit the shell
REPL Customization Commands:
Use these commands to customize the REPL behavior.
pref [get/set] Allows viewing or changing preferences, e.g. 'pref set editor.command.default 'C:\\Program Files\\Microsoft VS Code\\Code.exe'`
run Runs the script at the given path. A script is a set of commands that can be typed with one command per line
ui Displays the Swagger UI page, if available, in the default browser
Use `help <COMMAND>` for more detail on an individual command. e.g. `help get`.
For detailed tool info, see https://aka.ms/http-repl-doc.
HttpRepl 提供指令自動完成。 按 Tab 鍵會逐一查看完成您所鍵入之字元或 API 端點的命令清單。 下列各節將概述可用的 CLI 命令。
連線至網路 API
執行下列命令來連線至 web API:
httprepl <ROOT URI>
<ROOT URI> 是 web API 的基底 URI。 例如:
httprepl https://localhost:5001
或是在 HttpRepl 執行期間執行以下命令:
connect <ROOT URI>
例如:
(Disconnected)> connect https://localhost:5001
手動將指針指向 Web API 的 OpenAPI 描述
上述的連線命令將會嘗試自動尋找 OpenAPI 描述。 如果因為某些原因而無法這麼做,您可以使用 --openapi 選項來指定 Web API 的 OpenAPI 描述 URI:
connect <ROOT URI> --openapi <OPENAPI DESCRIPTION ADDRESS>
例如:
(Disconnected)> connect https://localhost:5001 --openapi /swagger/v1/swagger.json
啟用詳細資訊輸出,以取得 OpenAPI 描述搜尋、剖析和驗證的詳細資料
當工具搜尋 OpenAPI 描述、進行剖析並加以驗證時,使用 --verbose 命令指定 connect 選項將會產生更多詳細資料。
connect <ROOT URI> --verbose
例如:
(Disconnected)> connect https://localhost:5001 --verbose
Checking https://localhost:5001/swagger.json... 404 NotFound
Checking https://localhost:5001/swagger/v1/swagger.json... 404 NotFound
Checking https://localhost:5001/openapi.json... Found
Parsing... Successful (with warnings)
The field 'info' in 'document' object is REQUIRED [#/info]
The field 'paths' in 'document' object is REQUIRED [#/paths]
瀏覽網路 API
檢視可用的端點
若要列出 web API 位址目前路徑上的不同端點 (控制器),請執行 ls 或 dir 命令:
https://localhost:5001/> ls
下列輸出格式會隨即顯示:
. []
Fruits [get|post]
People [get|post]
https://localhost:5001/>
上述輸出代表有兩個控制器可用:Fruits 與 People。 兩個控制器均支援無參數的 HTTP GET 和 POST 作業。
瀏覽至特定控制器會顯示更多詳細資料。 舉例來說,以下命令的輸出會顯示 Fruits 控制器也支援 HTTP GET、PUT 和 DELETE 作業。 這些作業在路由中都需要 id 參數:
https://localhost:5001/fruits> ls
. [get|post]
.. []
{id} [get|put|delete]
https://localhost:5001/fruits>
或者,執行 ui 命令在瀏覽器中開啟 web API 的 Swagger UI 頁面。 例如:
https://localhost:5001/> ui
前往端點
若要瀏覽至 web API 上的不同端點,請執行 cd 命令:
https://localhost:5001/> cd people
接著 cd 命令的路徑不會區分大小寫。 下列輸出格式會隨即顯示:
/people [get|post]
https://localhost:5001/people>
自訂 HttpRepl
您可自訂 HttpRepl 的預設色彩。 此外,還可定義預設文字編輯器。 HttpRepl 喜好設定會在當前工作階段持續存在,且會沿用至後續的工作階段。 修改後,喜好設定會儲存在以下檔案中:
.httpreplprefs 檔案會於啟動時載入,且其變更不會於執行階段受到監視。 對檔案進行的手動修改只會在重新啟動工具後生效。
檢視設定
若要檢視可用的設定,請執行 pref get 命令。 例如:
https://localhost:5001/> pref get
上述命令會顯示可用的鍵值對:
colors.json=Green
colors.json.arrayBrace=BoldCyan
colors.json.comma=BoldYellow
colors.json.name=BoldMagenta
colors.json.nameSeparator=BoldWhite
colors.json.objectBrace=Cyan
colors.protocol=BoldGreen
colors.status=BoldYellow
設定色彩喜好設定
目前僅支援 JSON 的回應著色。 若要自訂 HttpRepl 工具的預設色彩,請找到與要變更之色彩相對應的鍵值。 如需如何尋找機碼的指示,請參閱檢視設定一節。 舉例來說,將 colors.json 機碼值從 Green 變更為 White,如下所示:
https://localhost:5001/people> pref set colors.json White
只能使用允許的色彩。 後續的 HTTP 請求會顯示新的著色輸出。
未設定特定色彩機碼時,會使用較泛用的機碼。 為了示範此遞補行為,請參考以下範例:
- 如果
colors.json.name沒有值,即使用colors.json.string。 - 如果
colors.json.string沒有值,即使用colors.json.literal。 - 如果
colors.json.literal沒有值,即使用colors.json。 - 如果
colors.json沒有值,即使用命令殼層的預設文字色彩 (AllowedColors.None)。
設定縮排大小
目前僅支援 JSON 的響應縮排大小自訂。 預設大小為兩個空格。 例如:
[
{
"id": 1,
"name": "Apple"
},
{
"id": 2,
"name": "Orange"
},
{
"id": 3,
"name": "Strawberry"
}
]
若要變更預設大小,請設定 formatting.json.indentSize 鍵。 舉例來說,若要一律使用四個空格:
pref set formatting.json.indentSize 4
後續回應皆會套用四個空格的設定:
[
{
"id": 1,
"name": "Apple"
},
{
"id": 2,
"name": "Orange"
},
{
"id": 3,
"name": "Strawberry"
}
]
設定預設文字編輯器
根據預設,HttpRepl 並未設定要使用的文字編輯器。 要測試需要 HTTP 請求本文的 Web API 方法,必須先設定預設文字編輯器。 HttpRepl 工具會啟動設定的文字編輯器,僅針對撰寫要求本文的目的使用。 請執行以下命令,來將您偏好的文字編輯器設為預設:
pref set editor.command.default "<EXECUTABLE>"
在上述命令中,<EXECUTABLE> 是文字編輯器可執行檔的完整路徑。 舉例來說,執行以下命令將 Visual Studio Code 設為預設文字編輯器:
若要以特定 CLI 引數啟動預設文字編輯器,請設定 editor.command.default.arguments 機碼。 假設 Visual Studio Code 是預設文字編輯器,且您希望 HttpRepl 在新的工作階段開啟 Visual Studio Code,但停用延伸模組。 執行以下命令:
pref set editor.command.default.arguments "--disable-extensions --new-window"
Tip
如果您的預設編輯器是 Visual Studio Code,您通常會想要傳遞 -w 或 --wait 引數,以強制 Visual Studio Code 先等候您關閉檔案再返回。
設定 OpenAPI 描述搜尋路徑
根據預設,HttpRepl 有一組相對路徑,在執行 connect 命令時 (不使用 --openapi 選項),HttpRepl 會用來尋找 OpenAPI 描述。 這些相對路徑會與 connect 命令中指定的根路徑和基本路徑結合。 預設的相對路徑為:
swagger.jsonswagger/v1/swagger.json/swagger.json/swagger/v1/swagger.jsonopenapi.json/openapi.json
若要在您的環境中使用一組不同的搜尋路徑,請設定 swagger.searchPaths 喜好設定。 此值必須是以直線分隔的相對路徑列表。 例如:
pref set swagger.searchPaths "swagger/v2/swagger.json|swagger/v3/swagger.json"
除了完全取代預設清單外,也可以藉由新增或移除路徑來修改清單。
若要將一或多個搜尋路徑新增至預設清單,請設定 swagger.addToSearchPaths 喜好設定。 此值必須是以直線分隔的相對路徑列表。 例如:
pref set swagger.addToSearchPaths "openapi/v2/openapi.json|openapi/v3/openapi.json"
若要將一或多個搜尋路徑從預設清單中移除,請設定 swagger.addToSearchPaths 喜好設定。 此值必須是以直線分隔的相對路徑列表。 例如:
pref set swagger.removeFromSearchPaths "swagger.json|/swagger.json"
測試 HTTP GET 要求
Synopsis
get <PARAMETER> [-F|--no-formatting] [-h|--header] [--response:body] [--response:headers] [-s|--streaming]
Arguments
PARAMETER
相關控制器動作方法預期的路由參數 (如果有的話)。
選項
以下是使用 get 命令時可用的選項:
-F|--no-formatting一個用於抑制 HTTP 回應格式的旗標。
-h|--header設定 HTTP 要求標頭。 支援下列兩種值格式:
{header}={value}{header}:{value}
--response:body指定一個檔案,用以寫入 HTTP 回應的內容。 例如:
--response:body "C:\response.json"。 如果檔案不存在,就會建立檔案。--response:headers指定 HTTP 回應標頭應寫入的檔案。 例如:
--response:headers "C:\response.txt"。 如果檔案不存在,就會建立檔案。-s|--streaming存在即啟用 HTTP 回應串流的旗幟。
Example
若要發出 HTTP GET 要求:
在支援的端點上執行
get命令:https://localhost:5001/people> get上述命令會顯示以下輸出格式:
HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 Date: Fri, 21 Jun 2019 03:38:45 GMT Server: Kestrel Transfer-Encoding: chunked [ { "id": 1, "name": "Scott Hunter" }, { "id": 2, "name": "Scott Hanselman" }, { "id": 3, "name": "Scott Guthrie" } ] https://localhost:5001/people>對
get命令傳遞參數來擷取特定記錄:https://localhost:5001/people> get 2上述命令會顯示以下輸出格式:
HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 Date: Fri, 21 Jun 2019 06:17:57 GMT Server: Kestrel Transfer-Encoding: chunked [ { "id": 2, "name": "Scott Hanselman" } ] https://localhost:5001/people>
測試 HTTP POST 要求
Synopsis
post <PARAMETER> [-c|--content] [-f|--file] [-h|--header] [--no-body] [-F|--no-formatting] [--response] [--response:body] [--response:headers] [-s|--streaming]
Arguments
PARAMETER
相關控制器動作方法預期的路由參數 (如果有的話)。
選項
-F|--no-formatting一個用於抑制 HTTP 回應格式的旗標。
-h|--header設定 HTTP 要求標頭。 支援下列兩種值格式:
{header}={value}{header}:{value}
--response:body指定一個檔案,用以寫入 HTTP 回應的內容。 例如:
--response:body "C:\response.json"。 如果檔案不存在,就會建立檔案。--response:headers指定 HTTP 回應標頭應寫入的檔案。 例如:
--response:headers "C:\response.txt"。 如果檔案不存在,就會建立檔案。-s|--streaming存在即啟用 HTTP 回應串流的旗幟。
-c|--content提供內聯 HTTP 請求本文。 例如:
-c "{\"id\":2,\"name\":\"Cherry\"}"。-f|--file提供包含 HTTP 請求正文的檔案路徑。 例如:
-f "C:\request.json"。--no-body不需要任何 HTTP 請求主體。
Example
若要發出 HTTP POST 要求:
在支援的端點上執行
post命令:https://localhost:5001/people> post -h Content-Type=application/json在上述命令中,
Content-TypeHTTP 要求標頭設為表示要求主體的媒體類型為 JSON。 預設文字編輯器會開啟 .tmp 檔案,其中包含代表 HTTP 要求本文的 JSON 範本。 例如:{ "id": 0, "name": "" }Tip
若要設定預設文字編輯器,請參閱設定預設文字編輯器一節。
修改 JSON 範本以滿足模型驗證需求:
{ "id": 0, "name": "Scott Addie" }儲存.tmp 檔案,然後關閉文字編輯器。 以下輸出會出現在命令列中:
HTTP/1.1 201 Created Content-Type: application/json; charset=utf-8 Date: Thu, 27 Jun 2019 21:24:18 GMT Location: https://localhost:5001/people/4 Server: Kestrel Transfer-Encoding: chunked { "id": 4, "name": "Scott Addie" } https://localhost:5001/people>
測試 HTTP PUT 要求
Synopsis
put <PARAMETER> [-c|--content] [-f|--file] [-h|--header] [--no-body] [-F|--no-formatting] [--response] [--response:body] [--response:headers] [-s|--streaming]
Arguments
PARAMETER
相關控制器動作方法預期的路由參數 (如果有的話)。
選項
-F|--no-formatting一個用於抑制 HTTP 回應格式的旗標。
-h|--header設定 HTTP 要求標頭。 支援下列兩種值格式:
{header}={value}{header}:{value}
--response:body指定一個檔案,用以寫入 HTTP 回應的內容。 例如:
--response:body "C:\response.json"。 如果檔案不存在,就會建立檔案。--response:headers指定 HTTP 回應標頭應寫入的檔案。 例如:
--response:headers "C:\response.txt"。 如果檔案不存在,就會建立檔案。-s|--streaming存在即啟用 HTTP 回應串流的旗幟。
-c|--content提供內聯 HTTP 請求本文。 例如:
-c "{\"id\":2,\"name\":\"Cherry\"}"。-f|--file提供包含 HTTP 請求正文的檔案路徑。 例如:
-f "C:\request.json"。--no-body不需要任何 HTTP 請求主體。
Example
若要發出 HTTP PUT 要求:
選擇性:執行
get命令以在修改前檢視資料:https://localhost:5001/fruits> get HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 Date: Sat, 22 Jun 2019 00:07:32 GMT Server: Kestrel Transfer-Encoding: chunked [ { "id": 1, "data": "Apple" }, { "id": 2, "data": "Orange" }, { "id": 3, "data": "Strawberry" } ]在支援的端點上執行
put命令:https://localhost:5001/fruits> put 2 -h Content-Type=application/json在上述命令中,
Content-TypeHTTP 要求標頭設為表示要求主體的媒體類型為 JSON。 預設文字編輯器會開啟 .tmp 檔案,其中包含代表 HTTP 要求本文的 JSON 範本。 例如:{ "id": 0, "name": "" }Tip
若要設定預設文字編輯器,請參閱設定預設文字編輯器一節。
修改 JSON 範本以滿足模型驗證需求:
{ "id": 2, "name": "Cherry" }儲存.tmp 檔案,然後關閉文字編輯器。 以下輸出會出現在命令列中:
[main 2019-06-28T17:27:01.805Z] update#setState idle HTTP/1.1 204 No Content Date: Fri, 28 Jun 2019 17:28:21 GMT Server: Kestrel選擇性:發出
get命令來查看修改。 舉例來說,如果您在文字編輯器中鍵入 "Cherry",get會傳回下列輸出:https://localhost:5001/fruits> get HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 Date: Sat, 22 Jun 2019 00:08:20 GMT Server: Kestrel Transfer-Encoding: chunked [ { "id": 1, "data": "Apple" }, { "id": 2, "data": "Cherry" }, { "id": 3, "data": "Strawberry" } ] https://localhost:5001/fruits>
測試 HTTP DELETE 要求
Synopsis
delete <PARAMETER> [-F|--no-formatting] [-h|--header] [--response] [--response:body] [--response:headers] [-s|--streaming]
Arguments
PARAMETER
相關控制器動作方法預期的路由參數 (如果有的話)。
選項
-F|--no-formatting一個用於抑制 HTTP 回應格式的旗標。
-h|--header設定 HTTP 要求標頭。 支援下列兩種值格式:
{header}={value}{header}:{value}
--response:body指定一個檔案,用以寫入 HTTP 回應的內容。 例如:
--response:body "C:\response.json"。 如果檔案不存在,就會建立檔案。--response:headers指定 HTTP 回應標頭應寫入的檔案。 例如:
--response:headers "C:\response.txt"。 如果檔案不存在,就會建立檔案。-s|--streaming存在即啟用 HTTP 回應串流的旗幟。
Example
若要發出 HTTP DELETE 要求:
選擇性:執行
get命令以在修改前檢視資料:https://localhost:5001/fruits> get HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 Date: Sat, 22 Jun 2019 00:07:32 GMT Server: Kestrel Transfer-Encoding: chunked [ { "id": 1, "data": "Apple" }, { "id": 2, "data": "Orange" }, { "id": 3, "data": "Strawberry" } ]在支援的端點上執行
delete命令:https://localhost:5001/fruits> delete 2上述命令會顯示以下輸出格式:
HTTP/1.1 204 No Content Date: Fri, 28 Jun 2019 17:36:42 GMT Server: Kestrel選擇性:發出
get命令來查看修改。 在本範例中,get會傳回下列輸出:https://localhost:5001/fruits> get HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 Date: Sat, 22 Jun 2019 00:16:30 GMT Server: Kestrel Transfer-Encoding: chunked [ { "id": 1, "data": "Apple" }, { "id": 3, "data": "Strawberry" } ] https://localhost:5001/fruits>
測試 HTTP PATCH 要求
Synopsis
patch <PARAMETER> [-c|--content] [-f|--file] [-h|--header] [--no-body] [-F|--no-formatting] [--response] [--response:body] [--response:headers] [-s|--streaming]
Arguments
PARAMETER
相關控制器動作方法預期的路由參數 (如果有的話)。
選項
-F|--no-formatting一個用於抑制 HTTP 回應格式的旗標。
-h|--header設定 HTTP 要求標頭。 支援下列兩種值格式:
{header}={value}{header}:{value}
--response:body指定一個檔案,用以寫入 HTTP 回應的內容。 例如:
--response:body "C:\response.json"。 如果檔案不存在,就會建立檔案。--response:headers指定 HTTP 回應標頭應寫入的檔案。 例如:
--response:headers "C:\response.txt"。 如果檔案不存在,就會建立檔案。-s|--streaming存在即啟用 HTTP 回應串流的旗幟。
-c|--content提供內聯 HTTP 請求本文。 例如:
-c "{\"id\":2,\"name\":\"Cherry\"}"。-f|--file提供包含 HTTP 請求正文的檔案路徑。 例如:
-f "C:\request.json"。--no-body不需要任何 HTTP 請求主體。
測試 HTTP HEAD 要求
Synopsis
head <PARAMETER> [-F|--no-formatting] [-h|--header] [--response] [--response:body] [--response:headers] [-s|--streaming]
Arguments
PARAMETER
相關控制器動作方法預期的路由參數 (如果有的話)。
選項
-F|--no-formatting一個用於抑制 HTTP 回應格式的旗標。
-h|--header設定 HTTP 要求標頭。 支援下列兩種值格式:
{header}={value}{header}:{value}
--response:body指定一個檔案,用以寫入 HTTP 回應的內容。 例如:
--response:body "C:\response.json"。 如果檔案不存在,就會建立檔案。--response:headers指定 HTTP 回應標頭應寫入的檔案。 例如:
--response:headers "C:\response.txt"。 如果檔案不存在,就會建立檔案。-s|--streaming存在即啟用 HTTP 回應串流的旗幟。
測試 HTTP OPTIONS 要求
Synopsis
options <PARAMETER> [-F|--no-formatting] [-h|--header] [--response] [--response:body] [--response:headers] [-s|--streaming]
Arguments
PARAMETER
相關控制器動作方法預期的路由參數 (如果有的話)。
選項
-F|--no-formatting一個用於抑制 HTTP 回應格式的旗標。
-h|--header設定 HTTP 要求標頭。 支援下列兩種值格式:
{header}={value}{header}:{value}
--response:body指定一個檔案,用以寫入 HTTP 回應的內容。 例如:
--response:body "C:\response.json"。 如果檔案不存在,就會建立檔案。--response:headers指定 HTTP 回應標頭應寫入的檔案。 例如:
--response:headers "C:\response.txt"。 如果檔案不存在,就會建立檔案。-s|--streaming存在即啟用 HTTP 回應串流的旗幟。
設定 HTTP 要求標頭
若要設定 HTTP 要求標頭,請使用下列其中一個方法:
與 HTTP 要求一同設定。 例如:
https://localhost:5001/people> post -h Content-Type=application/json若使用上述方法,則各相異的 HTTP 要求標頭都需要自己的
-h選項。於傳送 HTTP 要求之前設定。 例如:
https://localhost:5001/people> set header Content-Type application/json若在傳送要求之前設定標頭,該標頭會在命令提示字元工作階段的整個期間保持設定。 若要清除標頭,請提供空白值。 例如:
https://localhost:5001/people> set header Content-Type
測試受保護的端點
HttpRepl 支援以下列方式測試受保護的端點:
- 透過已登入使用者的預設認證。
- 透過使用 HTTP 要求標頭。
預設認證
假設您要測試某個 Web API,其裝載於 IIS 並受到 Windows 驗證所保護。 您希望執行工具的使用者所擁有的認證會流到要接受測試的 HTTP 端點。 若要傳遞已登入使用者的預設認證:
將
httpClient.useDefaultCredentials喜好設定設為true:pref set httpClient.useDefaultCredentials true先結束並重新啟動工具,再將另一個要求傳送至 Web API。
預設 Proxy 認證
假設您要測試的 Web API 位於受 Windows 驗證保護的 Proxy 後方。 您希望執行工具的使用者所擁有的認證流到 Proxy。 若要傳遞已登入使用者的預設認證:
將
httpClient.proxy.useDefaultCredentials喜好設定設為true:pref set httpClient.proxy.useDefaultCredentials true先結束並重新啟動工具,再將另一個要求傳送至 Web API。
HTTP 要求標頭
支援的驗證和授權配置範例包括:
- 基本身份驗證
- JWT 持有者令牌
- 摘要驗證
例如,您可以使用下列命令,將 Bearer Token 傳送至端點:
set header Authorization "bearer <TOKEN VALUE>"
若要存取 Azure 裝載的端點或要使用 Azure REST API,就需要有持有人權杖。 使用下列步驟,透過 Azure CLI 取得 Azure 訂用帳戶的持有人權杖。 HttpRepl 會在 HTTP 要求標頭中設定持有人權杖。 系統會擷取 Azure App Service Web Apps 清單。
登入 Azure:
az login使用下列命令取得您的訂用帳戶識別碼:
az account show --query id複製您的訂用帳戶識別碼並執行下列命令:
az account set --subscription "<SUBSCRIPTION ID>"使用下列命令取得持有人權杖:
az account get-access-token --query accessToken透過 HttpRepl 連線到 Azure REST API:
httprepl https://management.azure.com設定
AuthorizationHTTP 要求標頭:https://management.azure.com/> set header Authorization "bearer <ACCESS TOKEN>"前往訂閱
https://management.azure.com/> cd subscriptions/<SUBSCRIPTION ID>取得訂用帳戶的 Azure App Service Web Apps 清單:
https://management.azure.com/subscriptions/{SUBSCRIPTION ID}> get providers/Microsoft.Web/sites?api-version=2016-08-01會顯示下列回應:
HTTP/1.1 200 OK Cache-Control: no-cache Content-Length: 35948 Content-Type: application/json; charset=utf-8 Date: Thu, 19 Sep 2019 23:04:03 GMT Expires: -1 Pragma: no-cache Strict-Transport-Security: max-age=31536000; includeSubDomains X-Content-Type-Options: nosniff x-ms-correlation-request-id: <em>xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx</em> x-ms-original-request-ids: <em>xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx;xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx</em> x-ms-ratelimit-remaining-subscription-reads: 11999 x-ms-request-id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx x-ms-routing-request-id: WESTUS:xxxxxxxxxxxxxxxx:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx { "value": [ <AZURE RESOURCES LIST> ] }
切換 HTTP 請求顯示
根據預設,會隱藏所傳送之 HTTP 要求的顯示。 您可以修改命令提示字元工作階段的設定持續時間。
啟用要求顯示
透過執行 echo on 命令來檢視要傳送的 HTTP 要求。 例如:
https://localhost:5001/people> echo on
Request echoing is on
目前此工作階段中的後續 HTTP 請求會顯示請求標頭。 例如:
https://localhost:5001/people> post
[main 2019-06-28T18:50:11.930Z] update#setState idle
Request to https://localhost:5001...
POST /people HTTP/1.1
Content-Length: 41
Content-Type: application/json
User-Agent: HTTP-REPL
{
"id": 0,
"name": "Scott Addie"
}
Response from https://localhost:5001...
HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
Date: Fri, 28 Jun 2019 18:50:21 GMT
Location: https://localhost:5001/people/4
Server: Kestrel
Transfer-Encoding: chunked
{
"id": 4,
"name": "Scott Addie"
}
https://localhost:5001/people>
停用要求顯示
透過執行 echo off 命令來隱藏要傳送的 HTTP 要求顯示。 例如:
https://localhost:5001/people> echo off
Request echoing is off
執行指令碼
如果您經常執行一組相同的 HttpRepl 命令,請考慮將其儲存在文字檔中。 檔案中的命令會採用與手動在命令列上執行的命令相同的格式。 您可使用 run 命令以批次的方式執行命令。 例如:
建立包含一組以新行分隔命令的文字檔。 為了說明,請參考包含以下命令的 people-script.txt 檔案:
set base https://localhost:5001 ls cd People ls get 1執行
run命令,傳入文字檔的路徑。 例如:https://localhost:5001/> run C:\http-repl-scripts\people-script.txt會出現下列輸出:
https://localhost:5001/> set base https://localhost:5001 Using OpenAPI description at https://localhost:5001/swagger/v1/swagger.json https://localhost:5001/> ls . [] Fruits [get|post] People [get|post] https://localhost:5001/> cd People /People [get|post] https://localhost:5001/People> ls . [get|post] .. [] {id} [get|put|delete] https://localhost:5001/People> get 1 HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 Date: Fri, 12 Jul 2019 19:20:10 GMT Server: Kestrel Transfer-Encoding: chunked { "id": 1, "name": "Scott Hunter" } https://localhost:5001/People>
清除輸出
若要移除由 HttpRepl 工具寫入命令殼層的所有輸出,請執行 clear 或 cls 命令。 為了說明,請想像命令提示字元包含以下輸出:
httprepl https://localhost:5001
(Disconnected)> set base "https://localhost:5001"
Using OpenAPI description at https://localhost:5001/swagger/v1/swagger.json
https://localhost:5001/> ls
. []
Fruits [get|post]
People [get|post]
https://localhost:5001/>
執行以下命令來清除輸出:
https://localhost:5001/> clear
執行上述命令後,命令殼層只會包含以下輸出:
https://localhost:5001/>