Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2022 httpfile global variable for all files

We can create .http file in Visual Studio 2022 and call APIs. In each file we can defines a variable by using the syntax @VariableName=Value - for example:

@url = https://localhost:7000/api

Is it possible to declare @url once and use it in all .http files?

like image 755
M Komaei Avatar asked Oct 24 '25 05:10

M Komaei


1 Answers

We will create a file named httpenv.json. This file should be in the same folder as the HTTP file, or a folder above it. Visual Studio will look for that file in the folder where the HTTP file exists. If it’s not found, VS will look at through the parent directories to find it. When a file is found named httpenv.json, Visual Studio will stop searching for the file. The nearest file to the HTTP file found will be used. In this case I’ll add the httpenv.json to the same folder where the HTTP file is.

Note: Starting in 17.8 Preview 2, the names of the environment files will change to http-client.env.json and http-client.env.json.user. httpenv.json and httpenv.json.user will not be used after that.

{
  "dev": {
    "TemplatesApi_HostAddress": "localhost:44320",
    "searchTerm": "wpf"
  },
  "remote": {
    "TemplatesApi_HostAddress": "dotnetnew-api.azurewebsites.net",
    "searchTerm": "mads"
  }
}

enter image description here

https://devblogs.microsoft.com/visualstudio/safely-use-secrets-in-http-requests-in-visual-studio-2022/

My sample : enter image description here

enter image description here

*** important note : If you have change any value in http-client.env.json, you must change the combobox to and select it again or close the .http file and open it again because the values that are in http-client.env.json read when .http is open or the combobox change. (like changes in env path and terminal or powershell) or change the combobox to env: none and back to env: dev again.

My Sample :

http-client.env.json :

{
  "dev": {
    "url": "https://localhost:7000/api",
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpIjoiNyIsInVpIjoiYzVjYmE0OTctMjEzOS00NjA5LTk5YzUtN2E4Zjc3Y2Q5OTEwIiwiciI6InUifQ.td1A8hLgz-JmTkFFyxDE8EEEpDIwgSFAWZRCcButRa4"
  }
}

User.http :

@controller = User
###
GET {{url}}/{{controller}}/GetAll
Authorization: Bearer {{token}}
like image 96
M Komaei Avatar answered Oct 26 '25 18:10

M Komaei