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

[Feature Request] Support Response Scripting

Open loverde opened this issue 7 years ago • 11 comments

I think this is a consolidation of #86, #181, and #209, but it requests a variant that would be compatible with JetBrain's REST Client http file definitions (similar to #229 requesting support for rest-client.env.json for variables).

https://www.jetbrains.com/help/idea/http-client-in-product-code-editor.html

E.g.

GET /someplace_to_get_a_token

> {%
    var value = response.body.json.access_token;  
    // or more complex javascript logic to parse the response
    client.global.set("access_token", value);
%}

and support for the response scripts being external files:

> scripts/extract_access_token.js

The JetBrains API also supports client.test and client.assert for running tests/assertions on the response.

Regarding scoping of variables, it seems that JetBrains puts these in a transient scope that overrides the global values. But I'm not even sure how they handle viewing and resetting those values (you might have to restart the IDE).

So maybe it would be simpler to store them in the workspace rest-client.environmentVariables location for the currently selected environment (assuming it's possible to change those values from the VS Code API). That also has the benefit of preserving them and having a place to view and change them easily.

Or you could have a different API from JetBrains (see previous edit of this comment), but it would be nice to try to remain compatible where possible. It's kind of ugly, but maybe have a rest-client.globalSetterScope setting that determines where the client.global values get saved to.

If #21 is implemented to allow running multiple requests in a file at once, this feature would make that feature significantly more useful as well.

loverde avatar Aug 17 '18 17:08 loverde

Looks like VSCode does support changing the config via the API: https://github.com/Microsoft/vscode/blob/master/src/vs/vscode.d.ts#L3946

I guess the other option for storing as "transient" is to store it in ExtensionContext.workspaceState: https://github.com/Microsoft/vscode/blob/master/src/vs/vscode.d.ts#L4510

loverde avatar Aug 17 '18 18:08 loverde

@loverde nice suggestion, I considered the script support for a long time. I will update in this thread later.

Huachao avatar Aug 23 '18 05:08 Huachao

I love JetBrains REST client! Will be happy to have support here.

Do you need any help with the implementation?

werwolfby avatar Aug 24 '18 21:08 werwolfby

Yeah i would love to be able to extract values from json and then use as variables in upcoming call. This is the only thing that missing for me in this great extension.

EvgenyKhaliper avatar Nov 22 '18 11:11 EvgenyKhaliper

@EvgenyKhaliper your requirement is already well supported via request variables, or even file variables reference request variables. For more details, you can refer the README for more details

Huachao avatar Nov 22 '18 12:11 Huachao

@Huachao sorry, i've forgot to mention 'json' is an output of other REST call.

EvgenyKhaliper avatar Nov 22 '18 14:11 EvgenyKhaliper

Just in case it's not been posted, JetBrains has published a spec. There's other extensions for other editors that do similar "rest client" stuff, but it sure would be nice if all of them, including this one, would settle on a shared spec and the JetBrains spec is a good place to start. It would be great if there were also support for running these outside of the editor in our build pipelines, and with a "standardized" spec it wouldn't be hard to build such a tool.

wekempf avatar Dec 28 '18 15:12 wekempf

Echoing my support for something like this – in my case, I would love to use it to dynamically expand HATEOAS lists without having to deliberately build URLs. With request variables, you can get close, but I still find myself needing to extricate the query string from a valid HREF.

erodewald avatar Jun 12 '19 20:06 erodewald

This would be such a killer feature if it happened!

psygo avatar Aug 01 '23 11:08 psygo

I totally agree with @psygo Are there any update?

SeungwookHan avatar May 22 '24 05:05 SeungwookHan