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

JavaScript code evaluation

Open Maximus1285 opened this issue 5 years ago • 17 comments

It'd be nice to have the option of including JS code snippets to be resolved when sending the request

Maximus1285 avatar Feb 12 '20 08:02 Maximus1285

Yeah this would be really good. Postman's ability to run scripts pre-request is handy for doing simple things like setting dates in the future. I guess there is a slightly elaborate workaround for this in REST client. You could run a local server which executes your script and responds in json which the REST client file can extract the variables from?

davobutt avatar Feb 21 '20 16:02 davobutt

I am about to migrate from Postman. This is the only feature missing. I don't think running a local server would convince my team.

jnorkus avatar Feb 28 '20 15:02 jnorkus

I need to apply encription / decryption for all requests, so pre&post JS processing would be awesome!

xslim avatar Mar 31 '20 13:03 xslim

It would be POSTMAN-killer feature!

RimiX2 avatar Apr 03 '20 09:04 RimiX2

Yeah I would also like to have some scripting abilities.

robermorales avatar Apr 08 '20 16:04 robermorales

At least, some more complex variable processing to cover most of cases: like replacing only a substring of a variable, things like that. Some "templating" abilities can be enough.

robermorales avatar Apr 08 '20 16:04 robermorales

It would be handy, for JSON, to have a jq style post filter ability.

NickClark avatar Apr 16 '20 19:04 NickClark

I am looking for this feature then arrived here. It would be handy.

Add custom-filter or hooks during req & res duration.

maslow avatar Jul 19 '20 03:07 maslow

It would be handy, for JSON, to have a jq style post filter ability.

Yes, it would!! Ideally, integrated in the rest client output pane. Or a way to integrate or "pipe" to existing vscode jq extensions such as jq-vscode or VSCode jq playground.

Maybe we're not that far from such an integration: VSCode jq playground is already able to apply jq filters to any opened file or unsaved buffer, specified by its path or name. But it seems the rest client output pane is a different object which VSCode jq playground does not recognize: it looks like we'd just need an option in the rest client to send the json part of the rest call response to a given file.

afeblot avatar Aug 28 '20 23:08 afeblot

First of all, love this extension, I was searching some solution for code blocks in the extension, I just want to throw some ideas, maybe helpful or not.

Is it possible for variable resolution, using IIFE style ( similar to other ****Monkey chrome extensions) to wrap up user's custom code block?

e.g.

(function (param) {
    'use strict';

    /*
     * user custom code block goes here
     */

    return param + '->output';
})('input');

The similar feature in postman pre-request script

showenx avatar Nov 13 '20 08:11 showenx

on the home page for this extension it says "Generate code snippets for HTTP request in languages like Python, JavaScript and more" but have not seen any examples or documentation around this feature

asos-gurpreetsingh avatar Apr 22 '21 10:04 asos-gurpreetsingh

on the home page for this extension it says "Generate code snippets for HTTP request in languages like Python, JavaScript and more" but have not seen any examples or documentation around this feature

That is a feature that turns your request in to code that sends the same request.. If you do the "Rest Client: Generate code snippet" action you'll see what it does.

davobutt avatar Apr 22 '21 10:04 davobutt

I had an idea on this... What if we treated code blocks similarly to request blocks?

You give them a name, they are executed linearly top-to-bottom, with the stdout cached -- and if it can be parsed as JSON then you can treat it as JSON when referencing it later on.

We could even have some double-meaning with the language annotation:

  1. It gives us nice Markdown syntax highlighting
  2. It tells us what interpreter to use, effectively a #! shebang (defaulting to bash)

Example foo.http:


# @name randomData
```ruby
mac = 6.times.map { ("%02x" % rand(256)).upcase }.join(":")
name = ["dingus", "dorkus", "derp"].sample
puts({ mac: mac, name: name }.to_json)
```

###

POST localhost:3000/devices
content-type: application/json

{
  "name": {{randomData.name}},
  "macAddress": {{randomData.mac}}
}

kibiz0r avatar Nov 18 '21 17:11 kibiz0r

Any updates on this ? like running pre and post js code

piyushchauhan2011 avatar Mar 14 '24 04:03 piyushchauhan2011

@piyushchauhan2011, you can take a look at httpyac. I switched to that quite a while ago. It's more actively maintained and has more features. https://github.com/AnWeber/httpyac

asampal avatar Mar 14 '24 14:03 asampal

Thanks for the links @asampal . Hope restclient also add similar features, but httpyac seems to have good progress at present moment

piyushchauhan2011 avatar Mar 18 '24 09:03 piyushchauhan2011