zweifisch / ob-http

make http request within org-mode babel
GNU General Public License v3.0
256 stars 30 forks source link

[documentation] how to access auth token returned by the login request, in subsequent requests? #42

Closed bhanuvrat closed 5 years ago

bhanuvrat commented 5 years ago

I would like to pass the authentication token received in the login request to others. How can I set the output of one block as a variable to be used by others?

bhanuvrat commented 5 years ago

I had been searching all over the internet for days, and figured out right after creating the issue.. Particularly my lack of experience with org-babel syntax was the cause of delay.

Found the relevant help at http://howardism.org/Technical/Emacs/literate-programming-tutorial.html

Hoping that this will help someone else's hunting for the same answers.

#+NAME: login
#+BEGIN_SRC http :pretty json :select .auth_token :results value replace
POST http://localhost:8001/api/auth/login/
Content-Type: application/json

{
 "email": "someone.important@fakemail.com",
 "password": "plaintextpassword"
}
#+END_SRC

#+RESULTS: login
: eyJ0eXAiOiJKV1Q0_some_tokenish_value

*** profile fetch
#+BEGIN_SRC http :pretty :var auth_token=login
GET http://localhost:8001/api/me/
Authorization: Token ${auth_token}
#+END_SRC

#+RESULTS:
#+begin_example
{
  "id": "281ae67b-d9d4-4a2e-979a-8de5de029d48",
  "first_name": "someone",
  "last_name": "important",
  "email": "someone.important@fakemail.com",
  "gender": ""
}
#+end_example