I enjoy using org-babel for testing/exploring API HTTP endpoints.

#+BEGIN_SRC http :var access-token=select-access-token-from-sql :pretty
GET http://localhost:3000/api/items/71
Authorization: Bearer ${access-token}

71 is a hardcoded item id for the given(and only) user that I currently use for testing.

I would like to rewrite this snippet in a way to make it randomly picking a user(e.g. admin or a client), and based on user's ID or role request a specific item id(instead of hardcoded 71)

That's what I've tried and it didn't work:

#+name: sampleUserId
#+BEGIN_SRC emacs-lisp :results value
(defun sample (items)
  (let* ((size (length items))
         (index (random size)))
    (nth index items)))

(sample (list 11 22 33))

#+name: getResourceId
#+begin_src emacs-lisp :var userId=-1
(cond ((eq userId 11)
      ((eq userId 22)
      ((eq userId 33)
      (t "invalidUserId"))

#+BEGIN_SRC sh :var userId=sampleUserId :var resourceId=getResourceId(userId)
echo "Testing using User ID ${userId}, requested resource ID is ${resourceId}"

It fails with(probably as expected):

Reference userId not found in this buffer

Is there any approach to fix this issue? Perhaps instead of passing low-level userId this should be more high level construct consisting of user id and all of its item IDs? My other idea was to try using org-babel sessions for it, not sure if that's possible though.

The whole goal of this task is the ease of use, and cycling through roles/requests withoug leaving the SRC block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.