From ff1df4944ec2295071934772907181c343ae9378 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Mon, 3 Feb 2025 17:38:05 -1000 Subject: [PATCH 1/2] HTTP files request vars /1/new --- aspnetcore/test/http-files.md | 9 ++++++++- aspnetcore/test/http-files/samples/loginHttp.http | 13 +++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 aspnetcore/test/http-files/samples/loginHttp.http diff --git a/aspnetcore/test/http-files.md b/aspnetcore/test/http-files.md index b158391084ff..c64d4b677bcf 100644 --- a/aspnetcore/test/http-files.md +++ b/aspnetcore/test/http-files.md @@ -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 @@ -105,6 +105,13 @@ Accept-Language: en-US,en;q=0.5 ### ``` +## Request variables in HTTP files + + + + + :::code language="csharp" source="~/test/http-files/samples/loginHttp.http" highlight="1-2"::: + ## Comments Lines that start with either `#` or `//` are comments. These lines are ignored when Visual Studio sends HTTP requests. diff --git a/aspnetcore/test/http-files/samples/loginHttp.http b/aspnetcore/test/http-files/samples/loginHttp.http new file mode 100644 index 000000000000..f6c65e8a40f4 --- /dev/null +++ b/aspnetcore/test/http-files/samples/loginHttp.http @@ -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}} From 2026ce3499153a32d2e5b44ae997e0dc13475d0f Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Mon, 3 Feb 2025 17:57:02 -1000 Subject: [PATCH 2/2] HTTP files request vars /1/new --- aspnetcore/test/http-files.md | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/aspnetcore/test/http-files.md b/aspnetcore/test/http-files.md index c64d4b677bcf..17f3a1a2c7ff 100644 --- a/aspnetcore/test/http-files.md +++ b/aspnetcore/test/http-files.md @@ -105,18 +105,13 @@ Accept-Language: en-US,en;q=0.5 ### ``` -## Request variables in HTTP files - - - - - :::code language="csharp" source="~/test/http-files/samples/loginHttp.http" highlight="1-2"::: - ## Comments 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`. @@ -137,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: