-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Document request variables in .http files #34617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2aff18a
draft
tdykstra ad7082d
draft
tdykstra 99303ab
draft
tdykstra 4477438
draft
tdykstra e4efa4c
draft
tdykstra a9ad57a
draft
tdykstra 2541e9a
draft
tdykstra 470f957
Update aspnetcore/test/http-files.md
tdykstra 770007e
Update aspnetcore/test/http-files.md
tdykstra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |||||
|
|
||||||
| 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. | ||||||
|
|
||||||
| ## Request variables | ||||||
|
|
||||||
| You can pass values from one HTTP request to another within the same `.http` file. | ||||||
|
|
||||||
| 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`: | ||||||
|
|
||||||
| ```http | ||||||
| # @name login | ||||||
| https://contosol..com/api/login HTTP/1.1 | ||||||
| ``` | ||||||
|
|
||||||
| ```http | ||||||
| // @name login | ||||||
| https://contosol..com/api/login HTTP/1.1 | ||||||
tdykstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| ``` | ||||||
|
|
||||||
| 1. In subsequent requests in the same HTTP file use the request name to refer to the request. | ||||||
| 1. Use the following syntax to extract the specific part of the response that you want. | ||||||
|
|
||||||
| ```http | ||||||
| {{<request name>.(response|request).(body|headers).(*|JSONPath|XPath|<header name>)}}. | ||||||
| ``` | ||||||
|
|
||||||
| 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`). | ||||||
|
|
||||||
| When `body` is selected, the `*|JSONPath|XPath` part of the syntax applies: | ||||||
|
|
||||||
| * `*` extracts the entire response body. | ||||||
|
|
||||||
| Example: `{{login.response.body.*}}` | ||||||
|
|
||||||
| * For JSON responses, use [JSONPath](https://www.rfc-editor.org/rfc/rfc9535.html) to extract a specific property or attribute. | ||||||
|
|
||||||
| Example: `{{login.response.body.$.token}}` | ||||||
|
|
||||||
| * For XML responses, use [XPath](https://www.w3schools.com/xml/xpath_syntax.asp) to extract a specific property or attribute. | ||||||
|
|
||||||
| Example: `{{login.response.body./token}}` | ||||||
|
|
||||||
| When `headers` is selected, a header name extracts the entire header. Header names are case-insensitive. | ||||||
|
|
||||||
| Example: `{{login.response.headers.Location}}` | ||||||
|
|
||||||
| 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. | ||||||
|
|
||||||
| ### Example request variable usage | ||||||
|
|
||||||
| 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: | ||||||
|
|
||||||
| ```http | ||||||
| #@name login | ||||||
|
|
||||||
| POST {{TodoApi_HostAddress}}/users/token | ||||||
| Content-Type: application/json | ||||||
|
|
||||||
| { | ||||||
| "username": "{{myusername}}", | ||||||
|
||||||
| "username": "{{myusername}}", | |
| "username": "{{myusername}}" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.