You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/test/http-files.md
+77-1Lines changed: 77 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -190,6 +190,83 @@ A variable defined in an environment file can be the same as one defined in the
190
190
191
191
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.
192
192
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.
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:
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
+
193
270
## User-specific environment files
194
271
195
272
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
455
532
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:
456
533
457
534
* Request line that spans more than one line
458
-
* Named requests
459
535
* Specify file path as body of the request
460
536
* Mixed format for body when using multipart/form-data
0 commit comments