jreyesr / insomnia-plugin-batch-requests

An Insomnia plugin to repeatedly send a request, each time replacing some data with information from a CSV file, and collecting response data into the CSV file.
https://jreyesr.github.io/posts/insomnia-batch-requests/
MIT License
7 stars 0 forks source link

Extract HTTP status response code #12

Closed jochettino closed 3 months ago

jochettino commented 3 months ago

This plugin is amazing. I use it almost every single week and it's helping me to save time when I have to send tons of requests to the same endpoint with different parameters.

From the very beginning I started using it I missed a feature: it would be nice to extract the HTTP status response, so we could have a CSV after the execution as follows:

Parameter1,Parameter2,StatusResponse
hello,bye,200
hola,adios,200
ciao,ciao,200
failtest,failtest,500

Thanks for your work.

Regards, Jose

jreyesr commented 3 months ago

Hello @jochettino, and thanks a lot for the kind words!

I recall that being mentioned (briefly) on https://github.com/jreyesr/insomnia-plugin-batch-requests/discussions/3#discussion-4980083, a long time ago, but that conversation ultimately went another way and I never added that functionality.

Currently, the JSONPath expressions that can be used to save data from the response back to the CSV will always target the response body (i.e. the $ is always the response body, which must be JSON). I'm envisioning something like this: we add a dropdown menu to each Output, which lets you select from where will that data be pulled. For example, the response headers are another possible source of data on a response, apart from the body. And there's also the HTTP status code, which isn't a header, which is what you need. All those are different "data sources". Here's a quick idea that doesn't do anything yet:

image

Notice how there's a dropdown on the middle of each output. By default (i.e. on every newly added Output), it would be set to "From body", which is what it does now. On that state, the $.data.fullName expression would read from the JSON response. However, on the second output, the dropdown is set to "From header", and in that state the X-Entity-ID expression would read from a HTTP header like X-Entity-ID: 123.

Finally, what you asked for is at the bottom: the dropdown is set to "Status code". Notice how on that one we hide the JSONPath text field, since the status code is just the status code, there's no need to select anything from it.


Does that sound like it would solve your need? Or is it too complex? In such case, how did you imagine extracting the status code? In other words, what would you expect to write on the JSONPath field, so that it it returns the status code? As things are now, just writing $.statusCode would not work, since that would expect the response to be something like {"statusCode": 123}. I toyed with the idea of force adding a "magical" property to $, so something like $.__insomnia_status_code would automagically return the status code, but it feels a bit too obscure to me. The dropdowns are much more explicit, as just their existence encourages you to click on them and discover that there are other alternatives to retrieve data from. I'm curious as to other ways/UIs to expose data that isn't in the response body.

jochettino commented 3 months ago

Wow! Thanks for your quick answer.

I like your UI approach. Initially, I thought about having a "magical" property as you mentioned, but your proposal looks nice. Moreover, it adds the capability to extract the header response which I'm sure will be useful for others.

I can say your mock UI is easy to understand and doesn't overengineer the solution.

Regards!

jreyesr commented 3 months ago

Hello! Just to wrap this up, v1.4.0 was released yesterday. It includes a way to read output data from response headers, the status code, and the request time, in addition to the response body.

image

You should be able to update the plugin from Insomnia Preferences>Plugins (I hope, I haven't seen a button to do so). Worst case, you could uninstall and reinstall the plugin.

Feel free to try this out, and report any issues that you encounter. Otherwise, thanks for using the plugin and raising this issue!

jochettino commented 3 months ago

I tried it out this morning as I saw the change has been merged and a new version was generated. I celebrated with the team. The status code feature works like a charm.

Muchas gracias, amigo!