restclient.el icon indicating copy to clipboard operation
restclient.el copied to clipboard

Is it possible to execute two consecutive (synchronous) requests and pass params?

Open alexei-28 opened this issue 6 years ago • 5 comments

Here two independent requests:


:ORG_ID = 17
:PLACE_ID = 1
:ROOM_ID = 1
:TABLE_TOKEN = 11111111111
:USER = 33333
:USER_ID = 2

PUT :BASE/user?orgn=:ORG_ID&id=:USER_ID
Accept: application/json
Content-Type: application/json

{"active": false, "id": 2}

and second:

PUT :BASE/event?orgn=:ORG_ID&event=405&user=1
Accept: application/json
Content-Type: application/json

{"some": "test"}

to execute ,e.g. first request I put cursor inside request and press C-c C-c Nice. it's work fine.

But I need to execute both requests consecutive (synchronous) . Start second request only after first was finished.

  • Is it possible?
  • Is it possible the result of first request pass to second request?

alexei-28 avatar Nov 04 '19 12:11 alexei-28

No and no :)

pashky avatar Nov 04 '19 14:11 pashky

:-(

alexei-28 avatar Nov 04 '19 14:11 alexei-28

@alexei-28 - you can use org-babel with rest client to pass params into other rest client blocks. i have been starting up a ruby session to parse and save responses in named blocks which can then be passed as arguments to other rest client blocks. let me know if you'd like an example, it works nicely :)

gcentauri avatar Apr 10 '20 18:04 gcentauri

@alexei-28 - you can use org-babel with rest client to pass params into other rest client blocks. i have been starting up a ruby session to parse and save responses in named blocks which can then be passed as arguments to other rest client blocks. let me know if you'd like an example, it works nicely :)

Do you mean this package?

https://github.com/diadochos/org-babel-eval-in-repl

alexei-28 avatar Apr 10 '20 18:04 alexei-28

I mean this package https://github.com/alf/ob-restclient.el

here's an example:

*** POST request for admin login using Emacs restclient

    lets create a named org source block. this lets us call the code in this block later,
    so we can use the value it returns for something else, namely our authorization key
    for the other API requests. restclient is pretty simple. http verb, endpoint, headers
    and body, just like http.

    #+name: admin-login-request
    #+begin_src restclient :results value silent
      POST http://localhost:5000/api/v1/sessions
      Content-Type: application/json

      {
      "email": "[email protected]",
      "password": "password!"
      }
    #+end_src

    Don't evaluate this block directly, we'll call it next, when we start our ruby session.

*** Store the response in a Ruby session

    First lets just start a Ruby session and require the Json module.
    The :session keyword to this source block initiates a running
    repl that we can reference in other code blocks. Evaluate this block:

    #+begin_src ruby :session admin
      require 'json'
    #+end_src

    We can pass the JSON response from the request to a ruby source block. with
    babel we can speak many languages at once. using the :var header, we make 
    a variable named =response= and set it to be the value of executing the source
    block called =admin-login-request= that we wrote above.

    #+name: admin-key
    #+begin_src ruby :var response=admin-login-request() :session admin :results silent
      session_key ||= JSON.parse(response).dig("data", "attributes", "session_key")
    #+end_src

*** Pass the result of the login request to another restclient block

  This API has "organizations" which an admin can list. we set up a variable to use in the restclient code that uses the result of the `admin-key` ruby block.

    #+name: get-organization-index 
    #+begin_src restclient :var key=admin-key() :results value
      GET http://localhost:5000/api/v1/organizations
      Authorization: Bearer :key
      Content-Type: application/json
    #+end_src

gcentauri avatar Apr 10 '20 19:04 gcentauri