Huachao / vscode-restclient

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

Overwrite environment variable with value of request variable #179

Open OzgurDogan opened 6 years ago

OzgurDogan commented 6 years ago

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

Huachao commented 6 years ago

@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 commented 6 years ago

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

OzgurDogan commented 6 years ago

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

jahan01 commented 5 years ago

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

Huachao commented 5 years ago

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

RyanOC commented 5 years ago

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!

aymen-moukdad commented 5 years ago

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

RyanOC commented 5 years ago

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

slotakonrad commented 4 years ago

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

ghost commented 4 years ago

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.

msbenedetto commented 4 years ago

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.

RyanOC commented 4 years ago

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'
yupswing commented 3 years ago

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)

ImadMachi commented 2 years ago

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

ericksonlargura commented 2 years ago

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

yoeden commented 2 years ago

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

iraizo commented 2 years ago

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

airfortech commented 2 years ago

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

gabrielfmagalhaes commented 11 months ago

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

ejfasting commented 6 months ago

Hey, So, i guess this is a dead end and will never get implemented.. ? I would be very happy if it got added!

blogcraft commented 5 months ago

I have 1 file for sign up and retrieve a token and want to save it in an env to reuse that token it in my 100+ requests.

I do this already with postman, I need this feature to be able to ditch postman.

CengsBlanky commented 5 months ago

bruno(a open source REST client) already support env variable setting, example like this:

let status = res.getStatus();
if (201 == status) {
   let sys_token = res.getHeader("x-subject-token");
   bru.setEnvVar("system_token", sys_token);
}

every time I send a token request, it refresh my environment token and in other request I can use it as a normal environment variable

of course, this is achieved by adding a script language. And advanced user can write script to customize behavior pre-request or post-response

this plugin may not need to support script, but providing a way to access environment variables would be a huge improvement.

eLPe21 commented 5 months ago

6 years since the issue been opened and the only way to update token in environment is to do it manually?!? What a shame....

airfortech commented 5 months ago

last release 2y ago, so... What a pity, it is my fovourite rest plugin for vsc.

KevinDepedri commented 1 month ago

I agree that the ability to overwrite environment or file variables with the value of request variables is very important.

I'm encountering issues using RestClient to handle JWT authentication because, when a new access token is generated, it cannot overwrite the previously stored one. This makes it difficult to reference a single variable, such as {{accessTokenJwt}}, across multiple requests, especially when the token changes after a refresh.

This feature is much needed!