RESTer icon indicating copy to clipboard operation
RESTer copied to clipboard

Add value from the response to the environment

Open gadton opened this issue 7 years ago • 4 comments

I'm testing a system that uses an authentication based on a service. This login service returnes something like a session id as part of the response (JSON). This session id I have to send in subsequent requests as header value. Right now I have to add it manually to the environment after each login. Therefore I would like to have a simple way to add a value of the response to a given key in the environment.

gadton avatar Jan 30 '18 11:01 gadton

Hi @gadton. This is a great idea and it is definitely on my todo list.

I struggeling to come up with a good solution for it, which is why I postponed this idea. Lots of questions come to my mind:

  • Where do wou configure this?
    • Request: select an environment where to inject certain values of the response.
    • Environment: pull something out of recently made requests/responses.
  • How do you specify what part of the response you want to have?
    • You might want to pull values out of the response headers or body.
    • The body could be JSON, but also XML or something else.
    • A JSON path might work, e.g. body.data.0.session or headers.my-custom-data for a response like this:
      {
          "headers": {
              "content-type": "application/json",
              "my-custom-data": "bananas"
          },
          "body": {
              "data": [
                  { "session": "secret123!" }
              ]
          }
      }
      
    • This might be hard to understand, though. How should the UI for this look like?

Still, I do really like the idea. Apart from your login requirement, it could also make it possible to easily do a chain of dependend requests.

frigus02 avatar Jan 30 '18 20:01 frigus02

Hi @frigus02, here are some ideas:

  • a manual solution could be to just add a context menu to the response view providing an item "Set selection to environment variable" with a sub item for each environment variable
  • automated solution
    • I think the request is the right place
    • this would lead to something like response-triggers; each trigger should have
      • a check if it should be executed (e.g. status == 200)
      • an action
        • as you proposed +1
        • for the experienced: a few lines of JavaScript with access to request, response and env

Hope that helps

gadton avatar Jan 31 '18 07:01 gadton

Thanks a lot for your ideas. I like the response-triggers.

A few lines of JavaScript would be amazing. I'm not sure which security issues this might bring. How can you savely evaluate JavaScript in a browser extension, which has elevated permissions? I'm sure there are solutions for, though.

frigus02 avatar Jan 31 '18 20:01 frigus02

workaround after receive response you can copy session id to variable {SESSION_ID} and it will be used in all requests

OlegKyiashko avatar Aug 25 '21 11:08 OlegKyiashko