Huachao / vscode-restclient

REST Client Extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=humao.rest-client
MIT License
5.31k stars 444 forks source link

dotenv variables as environment variable value #489

Open tomdavidson opened 4 years ago

tomdavidson commented 4 years ago

dotenv variables do not resolve when used as environment variable value.

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!

Huachao commented 4 years ago

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

tomdavidson commented 4 years ago

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.

Huachao commented 4 years ago

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

skyred commented 4 years ago

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

LeCoupa commented 4 years ago

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

mjpoo commented 3 years ago

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

jackma8ge8 commented 3 years ago

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

LeCoupa commented 3 years ago

@jackma8ge8 like a normal .env

SECRET_TOKEN=mysecrettoken
jackma8ge8 commented 3 years ago

@LeCoupa thanks {{$dotenv SECRET_TOKEN}}

kent-id commented 3 years ago

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?

jackma8ge8 commented 3 years ago

@kent-id image

Usage

{{$dotenv HOST}}
{{$dotenv TOKEN}}
cgullcharlie commented 2 years ago

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

devguydavid commented 1 year ago

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 commented 1 year ago

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.

m3koenig commented 1 year ago

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

alexwaibel commented 2 months ago

This would be a really helpful feature for AAD auth flow as well. I'd love to be able to do something like this so I can safely commit this to our repo's .vscode/settings.json file:

{
    "rest-client.environmentVariables": {
        "$shared": {
            "aadV2ClientId": "00000000-0000-0000-0000-000000000000",
            "aadV2ClientSecret": "{{$dotenv CLIENT_SECRET}}",
            "aadV2TenantId": "00000000-0000-0000-0000-000000000000"
        },
        "dev": {
            "aadV2AppUri": "dev.example.com"
        },
        "prod": {
            "aadV2AppUri": "example.com"
        }
  }
}

That would let me add a dotenv template and gitignore the real dotenv file. Otherwise the best solution I've found is to put a placeholder secret there and add comments warning users not to commit their actual secrets on accident which is error prone.