使用 HttpRepl 來測試 Web API

HTTP「讀取、求值、輸出」迴圈 (REPL) 是:

  • 輕量型的跨平台命令列工具,其支援需求和 .NET Core 相同。
  • 用來提出 HTTP 要求來測試 ASP.NET Core Web API (及非 ASP.NET Core 的 Web API) 並檢視其結果。
  • 能夠測試裝載於任何環境中的 Web API,包括 localhost 和 Azure App Service。

支援的 HTTP 動詞命令如下:

若要跟著做,請檢視或下載範例 ASP.NET Core web API (如何下載)。

必要條件

安裝

若要安裝 HttpRepl,請執行下列命令:

dotnet tool install -g Microsoft.dotnet-httprepl

會從 Microsoft.dotnet-httprepl \(英文\) NuGet 套件安裝 .NET Core 全域工具

注意

根據預設,要安裝的 .NET 二進位檔架構代表目前執行的 OS 架構。 若要指定不同的 OS 架構,請參閱 dotnet tool install, --arch option。 如需詳細資訊,請參閱 GitHub 問題 dotnet/AspNetCore.Docs #29262

在 macOS 上更新路徑:

export PATH="$HOME/.dotnet/tools:$PATH"

使用方式

成功安裝工具後,請執行以下命令來啟動 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 命令。

連線至 web 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 描述、進行剖析並加以驗證時,使用 connect 命令指定 --verbose 選項將會產生更多詳細資料。

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]

檢視可用的端點

若要列出 web API 位址目前路徑上的不同端點 (控制器),請執行 lsdir 命令:

https://localhost:5001/> ls

下列輸出格式會隨即顯示:

.        []
Fruits   [get|post]
People   [get|post]

https://localhost:5001/>

上述輸出代表有兩個控制器可用:FruitsPeople。 兩個控制器均支援無參數的 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 喜好設定會存在目前的各工作階段,且會套用至後續的工作階段。 修改後,喜好設定會儲存在以下檔案中:

%HOME%/.httpreplprefs

.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 設為預設文字編輯器:

pref set editor.command.default "/usr/bin/code"

若要以特定 CLI 引數啟動預設文字編輯器,請設定 editor.command.default.arguments 機碼。 假設 Visual Studio Code 是預設文字編輯器,且您希望 HttpRepl 在新的工作階段開啟 Visual Studio Code,但停用延伸模組。 執行以下命令:

pref set editor.command.default.arguments "--disable-extensions --new-window"

提示

如果您的預設編輯器是 Visual Studio Code,您通常會想要傳遞 -w--wait 引數,以強制 Visual Studio Code 先等候您關閉檔案再返回。

設定 OpenAPI 描述搜尋路徑

根據預設,HttpRepl 有一組相對路徑,在執行 connect 命令時 (不使用 --openapi 選項),HttpRepl 會用來尋找 OpenAPI 描述。 這些相對路徑會與 connect 命令中指定的根路徑和基本路徑結合。 預設的相對路徑為:

  • swagger.json
  • swagger/v1/swagger.json
  • /swagger.json
  • /swagger/v1/swagger.json
  • openapi.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 要求

概要

get <PARAMETER> [-F|--no-formatting] [-h|--header] [--response:body] [--response:headers] [-s|--streaming]

引數

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 回應串流的旗標。

範例

若要發出 HTTP GET 要求:

  1. 在支援的端點上執行 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>
    
  2. 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 要求

概要

post <PARAMETER> [-c|--content] [-f|--file] [-h|--header] [--no-body] [-F|--no-formatting] [--response] [--response:body] [--response:headers] [-s|--streaming]

引數

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 POST 要求:

  1. 在支援的端點上執行 post 命令:

    https://localhost:5001/people> post -h Content-Type=application/json
    

    在上述命令中,Content-Type HTTP 要求標頭設定為指出 JSON 類型的要求本文媒體。 預設文字編輯器會開啟 .tmp 檔案,其中包含代表 HTTP 要求本文的 JSON 範本。 例如:

    {
      "id": 0,
      "name": ""
    }
    

    提示

    若要設定預設文字編輯器,請參閱設定預設文字編輯器一節。

  2. 修改 JSON 範本以滿足模型驗證需求:

    {
      "id": 0,
      "name": "Scott Addie"
    }
    
  3. 儲存.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 要求

概要

put <PARAMETER> [-c|--content] [-f|--file] [-h|--header] [--no-body] [-F|--no-formatting] [--response] [--response:body] [--response:headers] [-s|--streaming]

引數

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 PUT 要求:

  1. 選擇性:執行 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"
      }
    ]
    
  2. 在支援的端點上執行 put 命令:

    https://localhost:5001/fruits> put 2 -h Content-Type=application/json
    

    在上述命令中,Content-Type HTTP 要求標頭設定為指出 JSON 類型的要求本文媒體。 預設文字編輯器會開啟 .tmp 檔案,其中包含代表 HTTP 要求本文的 JSON 範本。 例如:

    {
      "id": 0,
      "name": ""
    }
    

    提示

    若要設定預設文字編輯器,請參閱設定預設文字編輯器一節。

  3. 修改 JSON 範本以滿足模型驗證需求:

    {
      "id": 2,
      "name": "Cherry"
    }
    
  4. 儲存.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
    
  5. 選擇性:發出 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 要求

概要

delete <PARAMETER> [-F|--no-formatting] [-h|--header] [--response] [--response:body] [--response:headers] [-s|--streaming]

引數

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 DELETE 要求:

  1. 選擇性:執行 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"
      }
    ]
    
  2. 在支援的端點上執行 delete 命令:

    https://localhost:5001/fruits> delete 2
    

    上述命令會顯示以下輸出格式:

    HTTP/1.1 204 No Content
    Date: Fri, 28 Jun 2019 17:36:42 GMT
    Server: Kestrel
    
  3. 選擇性:發出 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 要求

概要

patch <PARAMETER> [-c|--content] [-f|--file] [-h|--header] [--no-body] [-F|--no-formatting] [--response] [--response:body] [--response:headers] [-s|--streaming]

引數

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 要求

概要

head <PARAMETER> [-F|--no-formatting] [-h|--header] [--response] [--response:body] [--response:headers] [-s|--streaming]

引數

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 要求

概要

options <PARAMETER> [-F|--no-formatting] [-h|--header] [--response] [--response:body] [--response:headers] [-s|--streaming]

引數

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 端點。 若要傳遞已登入使用者的預設認證:

  1. httpClient.useDefaultCredentials 喜好設定設為 true

    pref set httpClient.useDefaultCredentials true
    
  2. 先結束並重新啟動工具,再將另一個要求傳送至 Web API。

預設 Proxy 認證

假設您要測試的 Web API 位於受 Windows 驗證保護的 Proxy 後方。 您希望執行工具的使用者所擁有的認證流到 Proxy。 若要傳遞已登入使用者的預設認證:

  1. httpClient.proxy.useDefaultCredentials 喜好設定設為 true

    pref set httpClient.proxy.useDefaultCredentials true
    
  2. 先結束並重新啟動工具,再將另一個要求傳送至 Web API。

HTTP 要求標頭

支援的驗證和授權配置範例包括:

  • basic authentication
  • JWT 持有人權杖
  • 摘要式驗證

例如,您可以使用下列命令,將持有人權杖傳送至端點:

set header Authorization "bearer <TOKEN VALUE>"

若要存取 Azure 裝載的端點或要使用 Azure REST API,就需要有持有人權杖。 使用下列步驟,透過 Azure CLI 取得 Azure 訂用帳戶的持有人權杖。 HttpRepl 會在 HTTP 要求標頭中設定持有人權杖。 系統會擷取 Azure App Service Web Apps 清單。

  1. 登入 Azure:

    az login
    
  2. 使用下列命令取得您的訂用帳戶識別碼:

    az account show --query id
    
  3. 複製您的訂用帳戶識別碼並執行下列命令:

    az account set --subscription "<SUBSCRIPTION ID>"
    
  4. 使用下列命令取得持有人權杖:

    az account get-access-token --query accessToken
    
  5. 透過 HttpRepl 連線到 Azure REST API:

    httprepl https://management.azure.com
    
  6. 設定 Authorization HTTP 要求標頭:

    https://management.azure.com/> set header Authorization "bearer <ACCESS TOKEN>"
    
  7. 瀏覽至訂用帳戶:

    https://management.azure.com/> cd subscriptions/<SUBSCRIPTION ID>
    
  8. 取得訂用帳戶的 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 命令以批次的方式執行命令。 例如:

  1. 建立包含一組以新行分隔命令的文字檔。 為了說明,請參考包含以下命令的 people-script.txt 檔案:

    set base https://localhost:5001
    ls
    cd People
    ls
    get 1
    
  2. 執行 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 工具寫入命令殼層的所有輸出,請執行 clearcls 命令。 為了說明,請參考包含以下輸出的命令殼層:

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/>

其他資源