Skip to content

Commit 27c00c8

Browse files
authored
Document request variables in .http files (#34617)
1 parent dd54e34 commit 27c00c8

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed

aspnetcore/test/http-files.md

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,83 @@ A variable defined in an environment file can be the same as one defined in the
190190

191191
In the preceding example, the `$shared` environment defines the `HostAddress` variable with the value `localhost:7293`. The `HostAddress` variable with the value `localhost:7293` functions as a default for environments that don't define a `HostAddress`. When the `dev1` or `dev2` environment is defined, the value for `HostAddress` comes from the `$shared` environment because `dev1` and `dev2` don't define a `HostAddress` variable. When the `staging` environment is defined, the value for `HostAddress` is set to `https://staging.contoso.com`, overriding the `$shared` default.
192192

193+
## Request variables
194+
195+
You can pass values from one HTTP request to another within the same `.http` file.
196+
197+
1. Create a single-line comment located just before a request URL to name the following request. For example, the following lines show alternative ways to name the request `login`:
198+
199+
```http
200+
# @name login
201+
https://contoso.com/api/login HTTP/1.1
202+
```
203+
204+
```http
205+
// @name login
206+
https://contoso.com/api/login HTTP/1.1
207+
```
208+
209+
1. In subsequent requests in the same HTTP file use the request name to refer to the request.
210+
1. Use the following syntax to extract the specific part of the response that you want.
211+
212+
```http
213+
{{<request name>.(response|request).(body|headers).(*|JSONPath|XPath|<header name>)}}.
214+
```
215+
216+
This syntax lets you extract values from the request itself or from the response to it (`request|response`). For either request or response, you can extract values from the body or the headers (`body|headers`).
217+
218+
When `body` is selected, the `*|JSONPath|XPath` part of the syntax applies:
219+
220+
* `*` extracts the entire response body.
221+
222+
Example: `{{login.response.body.*}}`
223+
224+
* For JSON responses, use [JSONPath](https://www.rfc-editor.org/rfc/rfc9535.html) to extract a specific property or attribute.
225+
226+
Example: `{{login.response.body.$.token}}`
227+
228+
* For XML responses, use [XPath](https://www.w3schools.com/xml/xpath_syntax.asp) to extract a specific property or attribute.
229+
230+
Example: `{{login.response.body./token}}`
231+
232+
When `headers` is selected, a header name extracts the entire header. Header names are case-insensitive.
233+
234+
Example: `{{login.response.headers.Location}}`
235+
236+
If you want to refer to the response of a named request, you need to manually trigger the named request to retrieve its response first. When you extract values from the response, you'll get the latest response if the request has been sent more than once.
237+
238+
### Example request variable usage
239+
240+
For example, suppose your HTTP file has a request that authenticates the caller, and you name it `login`. The response body is a JSON document that contains the bearer token in a property named `token`. In subsequent requests, you want to pass in this bearer token in an `Authorization` header. The following example does this:
241+
242+
```http
243+
#@name login
244+
245+
POST {{TodoApi_HostAddress}}/users/token
246+
Content-Type: application/json
247+
248+
{
249+
"username": "{{myusername}}",
250+
}
251+
252+
###
253+
254+
GET {{TodoApi_HostAddress}}/todos
255+
Authorization: Bearer {{login.response.body.$.token}}
256+
257+
###
258+
```
259+
260+
The syntax `{{login.response.body.$.token}}` represents the bearer token:
261+
262+
* **`login`**: Is the request name.
263+
* **`response`**: Refers to the HTTP response object.
264+
* **`body`**: Refers to the body of the HTTP response.
265+
* **`$`**: Represents the root element of the JSON document in the response body.
266+
* **`token`**: Refers to the specific property within the JSON document.
267+
268+
Without using request variables you would need to manually extract the token from the login response and include it in the header of subsequent requests. Request variables enable you to automate this process.
269+
193270
## User-specific environment files
194271

195272
A user-specific value is any value that a developer wants to test with but doesn't want to share with the team. The `http-client.env.json` file is checked in to source control by default, therefore, ***DO NOT*** add user-specific values to this file. Rather, add user-specific values in a file named `http-client.env.json.user`. The `http-client.env.json.user` file is located in the same folder as the `http-client.env.json` file. Files that end with `.user` are excluded from source control by default when using Visual Studio source control features.
@@ -455,7 +532,6 @@ Some of the preceding examples use the free open-source website <httpbin.org>. T
455532
The Visual Studio 2022 `.http` file editor doesn't have all the features that the Visual Studio Code [REST Client extension](https://marketplace.visualstudio.com/items?itemName=humao.rest-client) has. The following list includes some of the more significant features available only in the Visual Studio Code extension:
456533

457534
* Request line that spans more than one line
458-
* Named requests
459535
* Specify file path as body of the request
460536
* Mixed format for body when using multipart/form-data
461537
* GraphQL requests

0 commit comments

Comments
 (0)