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

Overwrite environment variable with value of request variable

Open OzgurDogan opened this issue 6 years ago • 25 comments

  • VSCode Version: 1.21.1
  • OS Version: Win10
  • REST Client Version: 0.18

After publishing the feature related to #140 I was expecting that the request in #86 was also going to be picked up. My request is much more generic than described in #86.

I would like to have the possibility to overwrite an environment variable from a value of an request variable.

So the following code would end up with updated value in my environment variable

# @name session
POST {{domain}}/v1/sessions HTTP/1.1
content-type: application/json
accept: application/json

{
    "userId": "{{userId}}",
    "password": "{{password}}"
}

{{token}} = {{session.response.body.token}}

In my example {{domain}} is a "workspace" setting, so it is shared within my team. {{userId}}, {{password}} and {{token}} are stored as user settings, so it is related to my account.

And by updating {{token}} I don't need to add this session request to all files and execute them one by one per file

OzgurDogan avatar Mar 29 '18 09:03 OzgurDogan

@OzgurDogan I'd like to consider this carefully, and think about the design carefully. And I will update in this thread later. Thanks for your suggstion

Huachao avatar Mar 29 '18 09:03 Huachao

@OzgurDogan in latest version 0.20.2, you can set the file variable value with references of request variables, like @token = {{session.response.body.token}}. Does this solve your problem?

Huachao avatar Oct 15 '18 07:10 Huachao

@Huachao unfortunately what I would like to achieve is to store te value in a setting file so it can be shared over files.

OzgurDogan avatar Oct 26 '18 08:10 OzgurDogan

@Huachao currently is there anyway to preview current assigned value from response body? it would nice to have a preview of current value. (in form of codelens or something)

jahan01 avatar Dec 09 '18 04:12 jahan01

@jahan01 you can hover over the request variable references like {{session.response.body.token}}

Huachao avatar Dec 09 '18 04:12 Huachao

Having the ability to overwrite an environmental variable is very important to me as well. I have dozens of requests and do not want to put my username/password in a post request in every file. I really need to have only one single file that has this authentication request and all the other files will read the environmental value that was set. Thanks for this awesome tool so far though!

RyanOC avatar Jan 21 '19 14:01 RyanOC

This is a very important feature. And it's absence is the reason why I'm still using Postman. The feature that is close to this one and already implemented in postman is : postman.setGlobalVariable("key", value);

aymen-moukdad avatar Jul 23 '19 15:07 aymen-moukdad

Same here @Croverit . This feature would be a huge win for this project IMO.

RyanOC avatar Jul 24 '19 14:07 RyanOC

Is this proposed option is planned to implement in the future? What are the barriers to overcome?

slotakonrad avatar Feb 10 '20 12:02 slotakonrad

I too am wanting to move from Postman to this extension, but the "environment" and "globals" variable issue, is something that would be decisive for me.

I have flows and within each request, I extract a different reference and then use this to call the next request.

Not being able to do this using REST API would prevent me from making the plunge and going all in, leaving Postman behind.

ghost avatar Feb 27 '20 10:02 ghost

Hi, I think this is what I need too. I want to set up a project with different workspaces linked all together by a settings.json but I want to be able to add/override certain variables only at workspace level (different client/different workspace, each one with a different name variable, etc). Can this be linked to: https://github.com/Huachao/vscode-restclient/issues/251 ? Great tool. Thanks.

msbenedetto avatar May 28 '20 20:05 msbenedetto

I created a powershell script that I can quickly call in the vscode terminal to have my token environment variable updated for now. Its working very well for me, and much better than copying it every time manually.

$env=$args[0]
$json = Get-Content '.vscode/settings.json' -raw | ConvertFrom-Json
$settings = $json.{rest-client.environmentVariables}
if($env -eq 'local'){
    $postParams = @{
        username = $settings.{local}.{userName};
        password = $settings.{local}.{password};
    }
    $uri = (-join($settings.{local}.{host}, 'post'))
    $response = Invoke-WebRequest -Uri $uri -Method POST -Body $postParams   
    $token = $response | ConvertFrom-Json | Select-Object access_token
    #Write-Host $token.psobject.properties.value
    $settings.{local}.{token} = $token.psobject.properties.value
} 
elseif($env -eq 'prod'){
    # todo: add request
} 
else {
    write-host("Environment undefined. Please try again.")   
}
$json | ConvertTo-Json | set-content '.vscode/settings.json'

RyanOC avatar Jun 24 '20 15:06 RyanOC

This extension is pretty great, but this feature is an essential one for many people workflows.

One way could be to allow us to rewrite an environment variable with a different syntax, like

@@environment_variable = "something"

to make it crystal clear we are not changing a file variable.

This could actually modify the vscode config file, or just be a temporary state until vscode restart (even better).

Two years have passed since this issue was open, are there any news? It makes this extension a missed replacement of postman for me (and many others as it appears)

yupswing avatar Mar 27 '21 08:03 yupswing

I've just moved from postman to rest-client, but got disappointed when I found out that there is no way to change environment variables, this issue was open for 3 years, and yet not implemented

ImadMachi avatar Apr 28 '22 23:04 ImadMachi

I was going to use rest-client, but without this feature it becomes unusable 😢

ericksonlargura avatar May 12 '22 17:05 ericksonlargura

Joining on board here, rooting for this feature to be implemented !

yoeden avatar Jul 03 '22 22:07 yoeden

how come this has not been added yet? this is completly essential to the whole client being actually useable, just sharing an auth token across the different files is literally impossible

iraizo avatar Sep 24 '22 23:09 iraizo

Any chance for add it? request variables would be great if only sharable via many files.

airfortech avatar Oct 08 '22 10:10 airfortech

Hey, how's everyone going? It's been another year since the last comment. Do we have any news? This feature would be really awesome

gabrielfmagalhaes avatar Nov 17 '23 16:11 gabrielfmagalhaes