vscode-restclient
vscode-restclient copied to clipboard
dotenv variables as environment variable value
dotenv variables do not resolve when used as environment variable value.
- VSCode Version: 1.41.1
- OS Version: kubuntu 19.10, 5.3.0-26
- REST Client Version: 0.23.0
Steps to Reproduce:
- create a .env file and add variables such as
PASSWORD_PRODUCTION=TkVuZtnoeEPYr
- create an environment variable in the workspace settings.json that references the dotenv file such as
"password": "{{$dotenv PASSWORD_PRODUCTION}}"
- In an .http file create a request that uses the environment variable such as ` "password": "{{password}}" in a POST body.
- generate a code snip it and observe '{{$dotenv PASSWORD_PRODUCTION}}' in the code rather than the value of the env var. 4.5 verify other non-dotenv environment variables are working as expected.
- create a file variable of the same name such as,
@password = {{$dotenv CMIX_PASSWORD_PRODUCTION}}
- generate a code snip it and observe the proper value in the code.
- move the .env file to the same dir as the .http file and repeat.
And, thank you, this is an awesome tool!
@tomdavidson do you mean support dotenv variables in extension environment variables?
Yes, that is what I meant in step #2. If that is not the intended design, then this would be a feature request and not a bug. I attempted to clarify in #418.
@tomdavidson I think this is a feature request and I've already added the feature request
label on the issue.
I can see this could be useful, or is there another to let variables in .env
to override the variables in settings.json
?
Yes, I was thinking about the same tonight. This feature would save me so much time
+1 for this feature request (if for no other reason than stopping GitGuardian warn me about example JWT tokens that I have in test authheaders).
How do I use this feature? Can you write a example. What the content format of .env file
@jackma8ge8 like a normal .env
SECRET_TOKEN=mysecrettoken
@LeCoupa thanks {{$dotenv SECRET_TOKEN}}
I'm trying to do the same thing but it doesn't seem to resolve to dotenv variable. Will you be willing to provide sample files so I can see how the settings.json / .env / .http files link to each other?
I reckon this repo can benefit from having couple of sample implementations written... something like: https://github.com/IdentityServer/IdentityServer4/tree/main/samples
--
oops... didn't notice this is still an open issue... so I guess not supported yet?
@kent-id
Usage
{{$dotenv HOST}}
{{$dotenv TOKEN}}
{{$dotenv HOST}} works {{$dotEnv HOST}} doesnt work. The documentation is incorrect
Maybe there is a different way to accomplish what I want to do. But I would love to see this. Here is why:
I have two different API keys, one for a production environment and one for a staging environment. I don't want to commit those to the .vscode/settings.json so I add them to a .env file.
At this point I want to be able to switch which API key is being used depending on which environment is selected. So ideally I would be able to dynamically change which .env variable is used when selecting a different environment. For example:
.env:
API_KEY_PRODUCTION=ABC
API_KEY_STAGING=DEF
settings.json:
{
"rest-client.environmentVariables": {
"$shared": {},
"staging": {
"apiKey": "{{$dotenv API_KEY_STAGING}}"
},
"production": {
"apiKey": "{{$dotenv API_KEY_PRODUCTION}}"
}
}
}
I could then use {{apiKey}}
within .http
files and it would use the correct api key value regardless of which environment is selected.
Is there another way to accomplish this same functionality?
Ok, something finally clicked right after I typed that above. I'll leave this here for others who might have the same question:
The indirect variable referencing method described in the docs for processEnv
using the '%' symbol works with dotenv, too:
.env:
API_KEY_PRODUCTION=ABC
API_KEY_STAGING=DEF
settings.json
{
"rest-client.environmentVariables": {
"$shared": {},
"staging": {
"apiKey": "API_KEY_STAGING"
},
"production": {
"apiKey": "API_KEY_PRODUCTION"
}
}
}
To be clear, those values are the literal strings "API_KEY_STAGING" and "API_KEY_PRODUCTION".
Then in an .http file:
POST https://example.com/thepath HTTP/1.1
X-Api-Key: {{$dotenv %apiKey}}
This will dynamically replace %apiKey
with either the string "API_KEY_STAGING" or "API_KEY_PRODUCTION" based on the environment selected, and then dotenv
resolves to using one of those from the .env
file.
@devguydavid @jackma8ge8 @cgullcharlie You three helped me to understand that feature! I was reading the docs many times and do not understand until I found this.
(except the "%"...I know that it is needed in this construct but not why :D)