Skip to content
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

HTTP files request vars /1/new #34629

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions aspnetcore/test/http-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: Learn how to use .http files in Visual Studio 2022 to test ASPNET C
monikerRange: '>= aspnetcore-8.0'
ms.topic: how-to
ms.author: tdykstra
ms.date: 01/19/2024
ms.date: 2/4/2024
uid: test/http-files
---
# Use .http files in Visual Studio 2022
Expand Down Expand Up @@ -109,7 +109,9 @@ Accept-Language: en-US,en;q=0.5

Lines that start with either `#` or `//` are comments. These lines are ignored when Visual Studio sends HTTP requests.

## Variables
## Request variables

HTTP request variables, also known as HTTP request parameters or query parameters, are key-value pairs sent by the client (e.g., web browser) to the server as part of an HTTP request. They can be used to send data and customize requests.

A line that starts with `@` defines a variable by using the syntax `@VariableName=Value`.

Expand All @@ -130,6 +132,16 @@ Variables can be defined using values of other variables that were defined earli
GET https://{{host}}/api/search/tool
```

When working with HTTP files, a common scenario is calling an endpoint, taking a value from the response, and sending in the value in a subsequent request. For example:

1. Call a sign in endpoint to authenticate a user and save the returned token.
1. Subsequent calls pass the token that was returned from the sign in endpoint.

Consider the following HTTP file:

:::code language="http" source="~/test/http-files/samples/loginHttp.http" highlight="1-2":::

In the preceding highlighted code, calling the `/users/token` endpoint authenticates the user
## Environment files

To give variables different values in different environments, create a file named `http-client.env.json`. Locate the file in the same directory as the `.http` file or in one of its parent directories. Here's an example of an environment file:
Expand Down
13 changes: 13 additions & 0 deletions aspnetcore/test/http-files/samples/loginHttp.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# @name login
POST {{TodoApi_HostAddress}}/users/token
Content-Type: application/json

{
"username": "{{myusername}}",
"password": "{{mypassword}}"
}

###

GET {{TodoApi_HostAddress}}/todos
Authorization: Bearer {{login.response.body.$.token}}