http参数 竖线_使用 HttpRepl 测试 Web API

使用 HttpRepl 测试 Web APITest web APIs with the HttpRepl

11/12/2020

本文内容

HTTP 读取–求值–打印循环 (REPL):The HTTP Read-Eval-Print Loop (REPL) is:

一种轻量级跨平台命令行工具,在所有支持的 .NET Core 的位置都可得到支持。A lightweight, cross-platform command-line tool that's supported everywhere .NET Core is supported.

用于发出 HTTP 请求以测试 ASP.NET Core Web API(和非 ASP.NET Core web API)并查看其结果。Used for making HTTP requests to test ASP.NET Core web APIs (and non-ASP.NET Core web APIs) and view their results.

可以在任何环境下测试托管的 web API,包括 localhost 和 Azure 应用服务。Capable of testing web APIs hosted in any environment, including localhost and Azure App Service.

The following HTTP verbs are supported:

先决条件Prerequisites

安装Installation

若要安装 HttpRepl,请运行以下命令:To install the HttpRepl, run the following command:

dotnet tool install -g Microsoft.dotnet-httprepl

使用情况Usage

成功安装该工具后,运行以下命令启动 HttpRepl:After successful installation of the tool, run the following command to start the HttpRepl:

httprepl

若要查看可用的 HttpRepl 命令,请运行以下其中一个命令:To view the available HttpRepl commands, run one of the following commands:

httprepl -h

httprepl --help

显示以下输出:The following output is displayed:

Usage:

httprepl [] [options]

Arguments:

- 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 ` for more detail on an individual command. e.g. `help get`.

For detailed tool info, see https://aka.ms/http-repl-doc.

HttpRepl 提供命令补全功能。The HttpRepl offers command completion. 按 Tab 键可循环访问补全所键入字符或 API 终结点的命令的列表。Pressing the Tab key iterates through the list of commands that complete the characters or API endpoint that you typed. 以下部分概述了可用的 CLI 命令。The following sections outline the available CLI commands.

连接到 Web APIConnect to the web API

运行以下命令,连接到 Web API:Connect to a web API by running the following command:

httprepl

是 Web API 的基 URI。 is the base URI for the web API. 例如:For example:

httprepl https://localhost:5001

或者,在运行 HttpRepl 的任何时候运行以下命令:Alternatively, run the following command at any time while the HttpRepl is running:

connect

例如:For example:

(Disconnected)> connect https://localhost:5001

手动指向 Web API 的 OpenAPI 说明Manually point to the OpenAPI description for the web API

上述 connect 命令将尝试自动查找 OpenAPI 说明。The connect command above will attempt to find the OpenAPI description automatically. 如果由于某种原因而无法执行此操作,则可以使用 --openapi 选项指定 Web API 的 OpenAPI 说明的 URI:If for some reason it's unable to do so, you can specify the URI of the OpenAPI description for the web API by using the --openapi option:

connect --openapi

例如:For example:

(Disconnected)> connect https://localhost:5001 --openapi /swagger/v1/swagger.json

启用详细输出,以获取有关 OpenAPI 说明搜索、分析和验证的详细信息Enable verbose output for details on OpenAPI description searching, parsing, and validation

当工具搜索 OpenAPI 说明并对其进行分析和验证时,通过 connect 命令指定 --verbose 选项将生成更多详细信息。Specifying the --verbose option with the connect command will produce more details when the tool searches for the OpenAPI description, parses, and validates it.

connect --verbose

例如:For example:

(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 APINavigate the web API

查看可用的终结点View available endpoints

若要在 Web API 地址的当前路径中列出不同的终结点(控制器),请运行 ls 或 dir 命令:To list the different endpoints (controllers) at the current path of the web API address, run the ls or dir command:

https://localhost:5001/> ls

以下输出格式随即显示:The following output format is displayed:

. []

Fruits [get|post]

People [get|post]

https://localhost:5001/>

上述输出指示有两个控制器可用:Fruits 和 People。The preceding output indicates that there are two controllers available: Fruits and People. 这两个控制器都支持无参数 HTTP GET 和 POST 操作。Both controllers support parameterless HTTP GET and POST operations.

导航到特定控制器可显示更多详细信息。Navigating into a specific controller reveals more detail. 例如,以下命令的输出显示 Fruits 控制器还支持 HTTP GET、PUT 和 DELETE 操作。For example, the following command's output shows the Fruits controller also supports HTTP GET, PUT, and DELETE operations. 其中每个操作都需要路由中有一个 id 参数:Each of these operations expects an id parameter in the route:

https://localhost:5001/fruits> ls

. [get|post]

.. []

{id} [get|put|delete]

https://localhost:5001/fruits>

或者,运行 ui 命令,在浏览器中打开 Web API 的 Swagger UI 页。Alternatively, run the ui command to open the web API's Swagger UI page in a browser. 例如:For example:

https://localhost:5001/> ui

导航到一个终结点Navigate to an endpoint

若要在 Web API 上导航到其他终结点,请运行 cd 命令:To navigate to a different endpoint on the web API, run the cd command:

https://localhost:5001/> cd people

cd 命令后的路径不区分大小写。The path following the cd command is case insensitive. 以下输出格式随即显示:The following output format is displayed:

/people [get|post]

https://localhost:5001/people>

自定义 HttpReplCustomize the HttpRepl

可自定义 HttpRepl 的默认颜色。The HttpRepl's default colors can be customized. 此外,还可定义默认文本编辑器。Additionally, a default text editor can be defined. HttpRepl 首选项在当前会话中保持不变,并且也会在之后的会话中使用。The HttpRepl preferences are persisted across the current session and are honored in future sessions. 修改后,首选项存储在以下文件中:Once modified, the preferences are stored in the following file:

%HOME%/.httpreplprefs%HOME%/.httpreplprefs

%HOME%/.httpreplprefs%HOME%/.httpreplprefs

%USERPROFILE%\.httpreplprefs%USERPROFILE%\.httpreplprefs

.httpreplprefs 文件将在启动时加载,并且在运行时不监控对其的更改。The .httpreplprefs file is loaded on startup and not monitored for changes at runtime. 对文件的手动修改只会在重启该工具后生效。Manual modifications to the file take effect only after restarting the tool.

查看设置View the settings

若要查看可用的设置,请运行 pref get 命令。To view the available settings, run the pref get command. 例如:For example:

https://localhost:5001/> pref get

上述命令显示可用的键值对:The preceding command displays the available key-value pairs:

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

设置颜色首选项Set color preferences

当前仅 JSON 支持响应着色。Response colorization is currently supported for JSON only. 若要自定义默认的 HttpRepl 工具着色,请找到与要更改的颜色相对应的键。To customize the default HttpRepl tool coloring, locate the key corresponding to the color to be changed. 有关如何查找键的说明,请参阅查看设置部分。For instructions on how to find the keys, see the View the settings section. 例如,将 colors.json 键值从 Green 更改为 White如下所示:For example, change the colors.json key value from Green to White as follows:

https://localhost:5001/people> pref set colors.json White

只能使用允许的颜色。Only the allowed colors may be used. 后续 HTTP 请求使用新着色显示输出。Subsequent HTTP requests display output with the new coloring.

如果未设置特定颜色键,则会考虑使用更通用的键。When specific color keys aren't set, more generic keys are considered. 若要演示此回退行为,请考虑使用以下示例:To demonstrate this fallback behavior, consider the following example:

如果 colors.json.name 没有值,则使用 colors.json.string。If colors.json.name doesn't have a value, colors.json.string is used.

如果 colors.json.string 没有值,则使用 colors.json.literal。If colors.json.string doesn't have a value, colors.json.literal is used.

如果 colors.json.literal 没有值,则使用 colors.json。If colors.json.literal doesn't have a value, colors.json is used.

如果 colors.json 没有值,则使用命令行界面的默认文本颜色 (AllowedColors.None)。If colors.json doesn't have a value, the command shell's default text color (AllowedColors.None) is used.

设置缩进尺寸Set indentation size

当前,仅 JSON 支持响应缩进尺寸自定义。Response indentation size customization is currently supported for JSON only. 默认尺寸为两个空格。The default size is two spaces. 例如:For example:

[

{

"id": 1,

"name": "Apple"

},

{

"id": 2,

"name": "Orange"

},

{

"id": 3,

"name": "Strawberry"

}

]

若要更改默认尺寸,请设置 formatting.json.indentSize 键。To change the default size, set the formatting.json.indentSize key. 例如,始终使用四个空格:For example, to always use four spaces:

pref set formatting.json.indentSize 4

后续响应遵循四个空格的设置:Subsequent responses honor the setting of four spaces:

[

{

"id": 1,

"name": "Apple"

},

{

"id": 2,

"name": "Orange"

},

{

"id": 3,

"name": "Strawberry"

}

]

设置默认文本编辑器Set the default text editor

默认情况下,HttpRepl 未配置任何文本编辑器供使用。By default, the HttpRepl has no text editor configured for use. 若要测试需要 HTTP 请求正文的 Web API 方法,必须设置默认文本编辑器。To test web API methods requiring an HTTP request body, a default text editor must be set. HttpRepl 工具将启动已配置的文本编辑器,专门用于编写请求正文。The HttpRepl tool launches the configured text editor for the sole purpose of composing the request body. 运行以下命令,将你青睐的文本编辑器设置为默认编辑器:Run the following command to set your preferred text editor as the default:

pref set editor.command.default ""

在上述命令中, 是文本编辑器可执行文件的完整路径。In the preceding command, is the full path to the text editor's executable file. 例如,运行以下命令,将 Visual Studio Code 设置为默认文本编辑器:For example, run the following command to set Visual Studio Code as the default text editor:

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

pref set editor.command.default "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"

pref set editor.command.default "C:\Program Files\Microsoft VS Code\Code.exe"

若要使用特定 CLI 参数启动默认文本编辑器,请设置 editor.command.default.arguments 键。To launch the default text editor with specific CLI arguments, set the editor.command.default.arguments key. 例如,假设 Visual Studio Code 为默认文本编辑器,并且你总是希望 HttpRepl 在禁用了扩展的新会话中打开 Visual Studio Code。For example, assume Visual Studio Code is the default text editor and that you always want the HttpRepl to open Visual Studio Code in a new session with extensions disabled. 运行下面的命令:Run the following command:

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

提示

如果默认编辑器是 Visual Studio Code,则通常需要传递 -w 或 --wait 参数以强制 Visual Studio Code 等待你关闭文件,然后再返回。If your default editor is Visual Studio Code, you'll usually want to pass the -w or --wait argument to force Visual Studio Code to wait for you to close the file before returning.

设置 OpenAPI 说明搜索路径Set the OpenAPI Description search paths

默认情况下,HttpRepl 具有一组相对路径,可用于在不使用 --openapi 选项执行 connect 命令时查找 OpenAPI 说明。By default, the HttpRepl has a set of relative paths that it uses to find the OpenAPI description when executing the connect command without the --openapi option. 将这些相对路径与 connect 命令中指定的根路径和基路径组合在一起。These relative paths are combined with the root and base paths specified in the connect command. 默认相对路径为:The default relative paths are:

swagger.jsonswagger.json

swagger/v1/swagger.jsonswagger/v1/swagger.json

/swagger.json/swagger.json

/swagger/v1/swagger.json/swagger/v1/swagger.json

openapi.jsonopenapi.json

/openapi.json/openapi.json

若要在环境中使用一组不同的搜索路径,请设置 swagger.searchPaths 首选项。To use a different set of search paths in your environment, set the swagger.searchPaths preference. 该值必须是以竖线分隔的相对路径列表。The value must be a pipe-delimited list of relative paths. 例如:For example:

pref set swagger.searchPaths "swagger/v2/swagger.json|swagger/v3/swagger.json"

除了替换默认列表外,还可以通过添加或删除路径来修改列表。Instead of replacing the default list altogether, the list can also be modified by adding or removing paths.

若要将一个或多个搜索路径添加到默认列表,请设置 swagger.addToSearchPaths 首选项。To add one or more search paths to the default list, set the swagger.addToSearchPaths preference. 该值必须是以竖线分隔的相对路径列表。The value must be a pipe-delimited list of relative paths. 例如:For example:

pref set swagger.addToSearchPaths "openapi/v2/openapi.json|openapi/v3/openapi.json"

若要从默认列表中删除一个或多个搜索路径,请设置 swagger.addToSearchPaths 首选项。To remove one or more search paths from the default list, set the swagger.addToSearchPaths preference. 该值必须是以竖线分隔的相对路径列表。The value must be a pipe-delimited list of relative paths. 例如:For example:

pref set swagger.removeFromSearchPaths "swagger.json|/swagger.json"

测试 HTTP GET 请求Test HTTP GET requests

摘要Synopsis

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

自变量Arguments

PARAMETER

关联控制器操作方法所需的路由参数(如果有)。The route parameter, if any, expected by the associated controller action method.

选项Options

get 命令可以使用以下选项:The following options are available for the get command:

-F|--no-formatting

禁用 HTTP 响应格式的标志。A flag whose presence suppresses HTTP response formatting.

-h|--header

设置 HTTP 请求标头。Sets an HTTP request header. 支持以下两种值格式:The following two value formats are supported:

{header}={value}

{header}:{value}

--response:body

指定一个文件,HTTP 响应正文应写入该文件。Specifies a file to which the HTTP response body should be written. 例如 --response:body "C:\response.json"。For example, --response:body "C:\response.json". 如果文件不存在,则创建该文件。The file is created if it doesn't exist.

--response:headers

指定一个文件,HTTP 响应标头应写入该文件。Specifies a file to which the HTTP response headers should be written. 例如 --response:headers "C:\response.txt"。For example, --response:headers "C:\response.txt". 如果文件不存在,则创建该文件。The file is created if it doesn't exist.

-s|--streaming

支持 HTTP 响应流式处理的标志。A flag whose presence enables streaming of the HTTP response.

示例Example

发出 HTTP GET 请求:To issue an HTTP GET request:

在支持 get 命令的终结点上运行该命令:Run the get command on an endpoint that supports it:

https://localhost:5001/people> get

上述命令显示以下输出格式:The preceding command displays the following output format:

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 命令来检索特定记录:Retrieve a specific record by passing a parameter to the get command:

https://localhost:5001/people> get 2

上述命令显示以下输出格式:The preceding command displays the following output format:

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 请求Test HTTP POST requests

摘要Synopsis

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

自变量Arguments

PARAMETER

关联控制器操作方法所需的路由参数(如果有)。The route parameter, if any, expected by the associated controller action method.

选项Options

-F|--no-formatting

禁用 HTTP 响应格式的标志。A flag whose presence suppresses HTTP response formatting.

-h|--header

设置 HTTP 请求标头。Sets an HTTP request header. 支持以下两种值格式:The following two value formats are supported:

{header}={value}

{header}:{value}

--response:body

指定一个文件,HTTP 响应正文应写入该文件。Specifies a file to which the HTTP response body should be written. 例如 --response:body "C:\response.json"。For example, --response:body "C:\response.json". 如果文件不存在,则创建该文件。The file is created if it doesn't exist.

--response:headers

指定一个文件,HTTP 响应标头应写入该文件。Specifies a file to which the HTTP response headers should be written. 例如 --response:headers "C:\response.txt"。For example, --response:headers "C:\response.txt". 如果文件不存在,则创建该文件。The file is created if it doesn't exist.

-s|--streaming

支持 HTTP 响应流式处理的标志。A flag whose presence enables streaming of the HTTP response.

-c|--content

提供内联 HTTP 请求正文。Provides an inline HTTP request body. 例如 -c "{"id":2,"name":"Cherry"}"。For example, -c "{"id":2,"name":"Cherry"}".

-f|--file

提供包含 HTTP 请求正文的文件的路径。Provides a path to a file containing the HTTP request body. 例如 -f "C:\request.json"。For example, -f "C:\request.json".

--no-body

指示无需 HTTP 请求正文。Indicates that no HTTP request body is needed.

示例Example

发出 HTTP POST 请求:To issue an HTTP POST request:

在支持 post 命令的终结点上运行该命令:Run the post command on an endpoint that supports it:

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

在上述命令中,Content-Type HTTP 请求标头被设置为指示 JSON 的请求正文媒体类型。In the preceding command, the Content-Type HTTP request header is set to indicate a request body media type of JSON. 默认文本编辑器打开一个 .tmp 文件,其中包含一个表示 HTTP 请求正文的 JSON 模板。The default text editor opens a .tmp file with a JSON template representing the HTTP request body. 例如:For example:

{

"id": 0,

"name": ""

}

提示

若要设置默认文本编辑器,请参阅设置默认文本编辑器部分。To set the default text editor, see the Set the default text editor section.

修改 JSON 模板以满足模型验证要求:Modify the JSON template to satisfy model validation requirements:

{

"id": 0,

"name": "Scott Addie"

}

保存 .tmp 文件,并关闭文本编辑器。Save the .tmp file, and close the text editor. 以下输出显示在命令行界面中:The following output appears in the command shell:

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 请求Test HTTP PUT requests

摘要Synopsis

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

自变量Arguments

PARAMETER

关联控制器操作方法所需的路由参数(如果有)。The route parameter, if any, expected by the associated controller action method.

选项Options

-F|--no-formatting

禁用 HTTP 响应格式的标志。A flag whose presence suppresses HTTP response formatting.

-h|--header

设置 HTTP 请求标头。Sets an HTTP request header. 支持以下两种值格式:The following two value formats are supported:

{header}={value}

{header}:{value}

--response:body

指定一个文件,HTTP 响应正文应写入该文件。Specifies a file to which the HTTP response body should be written. 例如 --response:body "C:\response.json"。For example, --response:body "C:\response.json". 如果文件不存在,则创建该文件。The file is created if it doesn't exist.

--response:headers

指定一个文件,HTTP 响应标头应写入该文件。Specifies a file to which the HTTP response headers should be written. 例如 --response:headers "C:\response.txt"。For example, --response:headers "C:\response.txt". 如果文件不存在,则创建该文件。The file is created if it doesn't exist.

-s|--streaming

支持 HTTP 响应流式处理的标志。A flag whose presence enables streaming of the HTTP response.

-c|--content

提供内联 HTTP 请求正文。Provides an inline HTTP request body. 例如 -c "{"id":2,"name":"Cherry"}"。For example, -c "{"id":2,"name":"Cherry"}".

-f|--file

提供包含 HTTP 请求正文的文件的路径。Provides a path to a file containing the HTTP request body. 例如 -f "C:\request.json"。For example, -f "C:\request.json".

--no-body

指示无需 HTTP 请求正文。Indicates that no HTTP request body is needed.

示例Example

发出 HTTP PUT 请求:To issue an HTTP PUT request:

可选:在修改之前,运行 get 命令以查看数据:Optional: Run the get command to view the data before modifying it:

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 命令的终结点上运行该命令:Run the put command on an endpoint that supports it:

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

在上述命令中,Content-Type HTTP 请求标头被设置为指示 JSON 的请求正文媒体类型。In the preceding command, the Content-Type HTTP request header is set to indicate a request body media type of JSON. 默认文本编辑器打开一个 .tmp 文件,其中包含一个表示 HTTP 请求正文的 JSON 模板。The default text editor opens a .tmp file with a JSON template representing the HTTP request body. 例如:For example:

{

"id": 0,

"name": ""

}

提示

若要设置默认文本编辑器,请参阅设置默认文本编辑器部分。To set the default text editor, see the Set the default text editor section.

修改 JSON 模板以满足模型验证要求:Modify the JSON template to satisfy model validation requirements:

{

"id": 2,

"name": "Cherry"

}

保存 .tmp 文件,并关闭文本编辑器。Save the .tmp file, and close the text editor. 以下输出显示在命令行界面中:The following output appears in the command shell:

[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 命令以查看修改。Optional: Issue a get command to see the modifications. 例如,如果在文本编辑器中键入了“Cherry”,则 get 会返回以下输出:For example, if you typed "Cherry" in the text editor, a get returns the following output:

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 请求Test HTTP DELETE requests

摘要Synopsis

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

自变量Arguments

PARAMETER

关联控制器操作方法所需的路由参数(如果有)。The route parameter, if any, expected by the associated controller action method.

选项Options

-F|--no-formatting

禁用 HTTP 响应格式的标志。A flag whose presence suppresses HTTP response formatting.

-h|--header

设置 HTTP 请求标头。Sets an HTTP request header. 支持以下两种值格式:The following two value formats are supported:

{header}={value}

{header}:{value}

--response:body

指定一个文件,HTTP 响应正文应写入该文件。Specifies a file to which the HTTP response body should be written. 例如 --response:body "C:\response.json"。For example, --response:body "C:\response.json". 如果文件不存在,则创建该文件。The file is created if it doesn't exist.

--response:headers

指定一个文件,HTTP 响应标头应写入该文件。Specifies a file to which the HTTP response headers should be written. 例如 --response:headers "C:\response.txt"。For example, --response:headers "C:\response.txt". 如果文件不存在,则创建该文件。The file is created if it doesn't exist.

-s|--streaming

支持 HTTP 响应流式处理的标志。A flag whose presence enables streaming of the HTTP response.

示例Example

发出 HTTP DELETE 请求:To issue an HTTP DELETE request:

可选:在修改之前,运行 get 命令以查看数据:Optional: Run the get command to view the data before modifying it:

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 命令的终结点上运行该命令:Run the delete command on an endpoint that supports it:

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

上述命令显示以下输出格式:The preceding command displays the following output format:

HTTP/1.1 204 No Content

Date: Fri, 28 Jun 2019 17:36:42 GMT

Server: Kestrel

可选:发出 get 命令以查看修改。Optional: Issue a get command to see the modifications. 在此示例中,get 返回以下输出:In this example, a get returns the following output:

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 请求Test HTTP PATCH requests

摘要Synopsis

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

自变量Arguments

PARAMETER

关联控制器操作方法所需的路由参数(如果有)。The route parameter, if any, expected by the associated controller action method.

选项Options

-F|--no-formatting

禁用 HTTP 响应格式的标志。A flag whose presence suppresses HTTP response formatting.

-h|--header

设置 HTTP 请求标头。Sets an HTTP request header. 支持以下两种值格式:The following two value formats are supported:

{header}={value}

{header}:{value}

--response:body

指定一个文件,HTTP 响应正文应写入该文件。Specifies a file to which the HTTP response body should be written. 例如 --response:body "C:\response.json"。For example, --response:body "C:\response.json". 如果文件不存在,则创建该文件。The file is created if it doesn't exist.

--response:headers

指定一个文件,HTTP 响应标头应写入该文件。Specifies a file to which the HTTP response headers should be written. 例如 --response:headers "C:\response.txt"。For example, --response:headers "C:\response.txt". 如果文件不存在,则创建该文件。The file is created if it doesn't exist.

-s|--streaming

支持 HTTP 响应流式处理的标志。A flag whose presence enables streaming of the HTTP response.

-c|--content

提供内联 HTTP 请求正文。Provides an inline HTTP request body. 例如 -c "{"id":2,"name":"Cherry"}"。For example, -c "{"id":2,"name":"Cherry"}".

-f|--file

提供包含 HTTP 请求正文的文件的路径。Provides a path to a file containing the HTTP request body. 例如 -f "C:\request.json"。For example, -f "C:\request.json".

--no-body

指示无需 HTTP 请求正文。Indicates that no HTTP request body is needed.

测试 HTTP HEAD 请求Test HTTP HEAD requests

摘要Synopsis

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

自变量Arguments

PARAMETER

关联控制器操作方法所需的路由参数(如果有)。The route parameter, if any, expected by the associated controller action method.

选项Options

-F|--no-formatting

禁用 HTTP 响应格式的标志。A flag whose presence suppresses HTTP response formatting.

-h|--header

设置 HTTP 请求标头。Sets an HTTP request header. 支持以下两种值格式:The following two value formats are supported:

{header}={value}

{header}:{value}

--response:body

指定一个文件,HTTP 响应正文应写入该文件。Specifies a file to which the HTTP response body should be written. 例如 --response:body "C:\response.json"。For example, --response:body "C:\response.json". 如果文件不存在,则创建该文件。The file is created if it doesn't exist.

--response:headers

指定一个文件,HTTP 响应标头应写入该文件。Specifies a file to which the HTTP response headers should be written. 例如 --response:headers "C:\response.txt"。For example, --response:headers "C:\response.txt". 如果文件不存在,则创建该文件。The file is created if it doesn't exist.

-s|--streaming

支持 HTTP 响应流式处理的标志。A flag whose presence enables streaming of the HTTP response.

测试 HTTP OPTIONS 请求Test HTTP OPTIONS requests

摘要Synopsis

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

自变量Arguments

PARAMETER

关联控制器操作方法所需的路由参数(如果有)。The route parameter, if any, expected by the associated controller action method.

选项Options

-F|--no-formatting

禁用 HTTP 响应格式的标志。A flag whose presence suppresses HTTP response formatting.

-h|--header

设置 HTTP 请求标头。Sets an HTTP request header. 支持以下两种值格式:The following two value formats are supported:

{header}={value}

{header}:{value}

--response:body

指定一个文件,HTTP 响应正文应写入该文件。Specifies a file to which the HTTP response body should be written. 例如 --response:body "C:\response.json"。For example, --response:body "C:\response.json". 如果文件不存在,则创建该文件。The file is created if it doesn't exist.

--response:headers

指定一个文件,HTTP 响应标头应写入该文件。Specifies a file to which the HTTP response headers should be written. 例如 --response:headers "C:\response.txt"。For example, --response:headers "C:\response.txt". 如果文件不存在,则创建该文件。The file is created if it doesn't exist.

-s|--streaming

支持 HTTP 响应流式处理的标志。A flag whose presence enables streaming of the HTTP response.

设置 HTTP 请求标头Set HTTP request headers

若要设置 HTTP 请求标头,请使用下面一种方法:To set an HTTP request header, use one of the following approaches:

使用该 HTTP 请求进行内联设置。Set inline with the HTTP request. 例如:For example:

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

若使用上述方法,每个不同的 HTTP 请求标头都需要其自己的 -h 选项。With the preceding approach, each distinct HTTP request header requires its own -h option.

在发送 HTTP 请求之前进行设置。Set before sending the HTTP request. 例如:For example:

https://localhost:5001/people> set header Content-Type application/json

在发送请求之前设置标头时,标头在命令行界面会话期间保持设置。When setting the header before sending a request, the header remains set for the duration of the command shell session. 若要清除标头,请提供一个空值。To clear the header, provide an empty value. 例如:For example:

https://localhost:5001/people> set header Content-Type

测试受保护的终结点Test secured endpoints

HttpRepl 支持通过以下方式测试受保护的终结点:The HttpRepl supports the testing of secured endpoints in the following ways:

通过已登录用户的默认凭据。Via the default credentials of the logged in user.

通过使用 HTTP 请求头。Through the use of HTTP request headers.

默认凭据Default credentials

假设你正在测试的 Web API 托管在 IIS 中,并使用 Windows 身份验证进行保护。Consider a web API you're testing that's hosted in IIS and secured with Windows authentication. 你希望正在运行该工具的用户的凭据流向正在进行测试的 HTTP 终结点。You want the credentials of the user running the tool to flow across to the HTTP endpoints being tested. 若要传递已登录用户的默认凭据,请执行以下操作:To pass the default credentials of the logged in user:

将 httpClient.useDefaultCredentials 首选项设置为 true:Set the httpClient.useDefaultCredentials preference to true:

pref set httpClient.useDefaultCredentials true

退出并重启该工具,然后将另一个请求发送到 Web API。Exit and restart the tool before sending another request to the web API.

默认代理凭据Default proxy credentials

假设你正在测试的 Web API 位于使用 Windows 身份验证保护的代理后面。Consider a scenario in which the web API you're testing is behind a proxy secured with Windows authentication. 你希望正在运行该工具的用户的凭据流向该代理。You want the credentials of the user running the tool to flow to the proxy. 若要传递已登录用户的默认凭据,请执行以下操作:To pass the default credentials of the logged in user:

将 httpClient.proxy.useDefaultCredentials 首选项设置为 true:Set the httpClient.proxy.useDefaultCredentials preference to true:

pref set httpClient.proxy.useDefaultCredentials true

退出并重启该工具,然后将另一个请求发送到 Web API。Exit and restart the tool before sending another request to the web API.

HTTP 请求标头HTTP request headers

支持的身份验证和授权方案的示例包括:Examples of supported authentication and authorization schemes include:

basic authenticationbasic authentication

JWT 持有者令牌JWT bearer tokens

摘要式身份验证digest authentication

例如,可以使用以下命令将持有者令牌发送到终结点:For example, you can send a bearer token to an endpoint with the following command:

set header Authorization "bearer "

若要访问 Azure 托管的终结点或使用 Azure REST API,你需要持有者令牌。To access an Azure-hosted endpoint or to use the Azure REST API, you need a bearer token. 使用以下步骤,通过 Azure CLI 来获取 Azure 订阅的持有者令牌。Use the following steps to obtain a bearer token for your Azure subscription via the Azure CLI. HttpRepl 在 HTTP 请求头中设置持有者令牌。The HttpRepl sets the bearer token in an HTTP request header. 检索 Azure 应用服务 Web 应用的列表。A list of Azure App Service Web Apps is retrieved.

登录 Azure:Sign in to Azure:

az login

使用以下命令获取订阅 ID:Get your subscription ID with the following command:

az account show --query id

复制订阅 ID 并运行以下命令:Copy your subscription ID and run the following command:

az account set --subscription ""

使用以下命令获取持有者令牌:Get your bearer token with the following command:

az account get-access-token --query accessToken

通过 HttpRepl 连接到 Azure REST API:Connect to the Azure REST API via the HttpRepl:

httprepl https://management.azure.com

设置 Authorization HTTP 请求标头:Set the Authorization HTTP request header:

https://management.azure.com/> set header Authorization "bearer "

导航到订阅:Navigate to the subscription:

https://management.azure.com/> cd subscriptions/

获取订阅的 Azure 应用服务 Web 应用的列表:Get a list of your subscription's Azure App Service Web Apps:

https://management.azure.com/subscriptions/{SUBSCRIPTION ID}> get providers/Microsoft.Web/sites?api-version=2016-08-01

将显示以下响应:The following response is displayed:

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: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

x-ms-original-request-ids: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx;xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

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": [

]

}

切换 HTTP 请求显示Toggle HTTP request display

默认情况下,禁止显示正在发送的 HTTP 请求。By default, display of the HTTP request being sent is suppressed. 可在命令行界面会话期间更改相应的设置。It's possible to change the corresponding setting for the duration of the command shell session.

启用请求显示Enable request display

运行 echo on 命令可查看正在发送的 HTTP 请求。View the HTTP request being sent by running the echo on command. 例如:For example:

https://localhost:5001/people> echo on

Request echoing is on

当前会话中的后续 HTTP 请求显示请求标头。Subsequent HTTP requests in the current session display the request headers. 例如:For example:

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>

禁用请求显示Disable request display

运行 echo off 命令可禁止显示正在发送的 HTTP 请求。Suppress display of the HTTP request being sent by running the echo off command. 例如:For example:

https://localhost:5001/people> echo off

Request echoing is off

运行脚本Run a script

如果经常执行一组相同的 HttpRepl 命令,请考虑将它们存储在一个文本文件中。If you frequently execute the same set of HttpRepl commands, consider storing them in a text file. 文件中的命令采用与在命令行上手动执行的命令相同的形式。Commands in the file take the same form as commands executed manually on the command line. 可使用 run 命令批量执行这些命令。The commands can be executed in a batched fashion using the run command. 例如:For example:

创建一个文本文件,其中包含一组换行符分隔的命令。Create a text file containing a set of newline-delimited commands. 例如,一个包含以下命令的 people-script.txt 文件:To illustrate, consider a people-script.txt file containing the following commands:

set base https://localhost:5001

ls

cd People

ls

get 1

执行 run 命令,传入文本文件的路径。Execute the run command, passing in the text file's path. 例如:For example:

https://localhost:5001/> run C:\http-repl-scripts\people-script.txt

随即显示以下输出:The following output appears:

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>

清除输出Clear the output

若要删除 HttpRepl 工具写入命令行界面的所有输出,请运行 clear 或 cls 命令。To remove all output written to the command shell by the HttpRepl tool, run the clear or cls command. 例如,假设命令行界面包含以下输出:To illustrate, imagine the command shell contains the following output:

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

运行以下命令以清除输出:Run the following command to clear the output:

https://localhost:5001/> clear

运行上述命令后,命令行界面仅包含以下输出:After running the preceding command, the command shell contains only the following output:

https://localhost:5001/>

其他资源Additional resources

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值