riok / Kreya

Kreya is a GUI client for gRPC and REST APIs with innovative features for environments, authorizations and more.
https://kreya.app
287 stars 5 forks source link

Scripting to re-use data in chains of requests #23

Closed bdrupieski closed 2 years ago

bdrupieski commented 3 years ago

Is your feature request related to a problem? Please describe.

I often make chains of requests where I need to feed in unique data into the first request, then copy a value from its response to paste into another request. Copying and pasting values is tedious and annoying, and slows down ad-hoc testing of gRPC endpoints I'm developing.

For example suppose I'm making two calls, one to first create some entity and then a second to update it. I need to pass in some unique id to the first request. Currently I can use Scriban to generate a unique value using {{ date.now | date.to_string '%Y-%m-%dT%H:%M:%S:%L%Z' }}, which I can then base64 encode if I want to make it a relatively unique string. I saw support was recently added for math.uuid and math.random, so maybe I can use those soon instead of using date.now. However, I can't store the generated string in the environment or user-specific data, so I can't refer to it in future requests. Also, if I get an id back in the response, there's no way to parse the JSON in the response to get the value for the id and store it somewhere to use it as input into the next request.

Describe the solution you'd like

In Postman for plain JSON over HTTP1.1 API calls I can automate this by using its pre-request ("Pre-request Script") and post-response ("Tests") scripting to get and set variables.

For example I can generate a uuid using a pre-request script like this:

var uuid = require('uuid');
pm.environment.set("id", uuid.v4());

And then similar to Scriban templating in Kreya I can refer to the environment variable I generated using {{id}} in the body of the request.

Also, after a response I can parse the JSON to pull out data and save it as an environment variable to use in the next request.

I'm looking for some solution like the above that allows me to get and set data before requests and after responses so I can use that data across different calls.

Describe alternatives you've considered

The JSON path filtering in the response at least makes it easier to filter down the response body so I don't need to hunt through many lines of JSON to find the exact value I need to copy and paste into the next request. I still need to copy and paste it myself though.

It looks like Scriban supports assigning to variables, so after generating a value from {{ date.now | date.to_string '%Y-%m-%dT%H:%M:%S:%L%Z' }} I tried to set it as a property of an object in the environment data. It doesn't seem to be persisted though. It seems that data gets copied into whatever the context is for the template and I can make whatever changes I want to it in there, but any changes to it don't make their way back out.

Additional context

This is a pretty sweet tool, thank you for building this and making it available! If it was open source I'd definitely start looking into how I could contribute to a feature like this. I feel a little bad that I can only create an issue for this and can't help to make it happen.

CommonGuy commented 3 years ago

Cool ideas! We plan to implement this soon(ish).

Don't feel bad for creating an issue, we are very happy that we receive such detailed feature requests and that you find Kreya useful! Especially since it is our own decision to make Kreya closed source (see https://github.com/riok/Kreya/issues/21#issuecomment-800035637), we'll have to live with the consequences :)

CommonGuy commented 2 years ago

With the Kreya 1.9 release, we introduced user variables, which allows to re-use data from other operations. For more information, see https://kreya.app/docs/scripting-and-tests/#user-variables.

Note: This feature is only available via scripting, which requires a Pro or Enterprise plan.