vscode-restclient icon indicating copy to clipboard operation
vscode-restclient copied to clipboard

dotenv variables as environment variable value

Open tomdavidson opened this issue 5 years ago • 16 comments

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:

  1. create a .env file and add variables such as PASSWORD_PRODUCTION=TkVuZtnoeEPYr
  2. create an environment variable in the workspace settings.json that references the dotenv file such as "password": "{{$dotenv PASSWORD_PRODUCTION}}"
  3. In an .http file create a request that uses the environment variable such as ` "password": "{{password}}" in a POST body.
  4. 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.
  5. create a file variable of the same name such as, @password = {{$dotenv CMIX_PASSWORD_PRODUCTION}}
  6. generate a code snip it and observe the proper value in the code.
  7. move the .env file to the same dir as the .http file and repeat.

And, thank you, this is an awesome tool!

tomdavidson avatar Jan 16 '20 18:01 tomdavidson

@tomdavidson do you mean support dotenv variables in extension environment variables?

Huachao avatar Jan 17 '20 04:01 Huachao

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 avatar Jan 17 '20 05:01 tomdavidson

@tomdavidson I think this is a feature request and I've already added the feature request label on the issue.

Huachao avatar Jan 17 '20 05:01 Huachao

I can see this could be useful, or is there another to let variables in .env to override the variables in settings.json?

skyred avatar Jul 28 '20 01:07 skyred

Yes, I was thinking about the same tonight. This feature would save me so much time

LeCoupa avatar Nov 15 '20 19:11 LeCoupa

+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).

mjpoo avatar Mar 09 '21 13:03 mjpoo

How do I use this feature? Can you write a example. What the content format of .env file

jackma8ge8 avatar Apr 20 '21 04:04 jackma8ge8

@jackma8ge8 like a normal .env

SECRET_TOKEN=mysecrettoken

LeCoupa avatar Apr 20 '21 09:04 LeCoupa

@LeCoupa thanks {{$dotenv SECRET_TOKEN}}

jackma8ge8 avatar Apr 22 '21 07:04 jackma8ge8

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 avatar Apr 26 '21 05:04 kent-id

@kent-id image

Usage

{{$dotenv HOST}}
{{$dotenv TOKEN}}

jackma8ge8 avatar Apr 28 '21 13:04 jackma8ge8

{{$dotenv HOST}} works {{$dotEnv HOST}} doesnt work. The documentation is incorrect

cgullcharlie avatar Feb 23 '22 10:02 cgullcharlie

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?

devguydavid avatar Nov 17 '22 00:11 devguydavid

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 avatar Nov 17 '22 00:11 devguydavid

@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)

m3koenig avatar Feb 01 '23 10:02 m3koenig