HexmosTech / Lama2

Free, Lightweight & Collaborative API Client
https://hexmos.com/lama2/index.html
GNU Affero General Public License v3.0
109 stars 6 forks source link

How to make use of variable to remember the response and use it in subsequence command? #10

Open geohuz opened 1 year ago

geohuz commented 1 year ago

For application using JWT token this is the typical use case, like this in my current curl script:

TOKEN=$(curl   -d '{"email": "vicky@gmail.com", "pass": "hello"}' -H 'Content-Type: application/json'  http://localhost:3210/rpc/login | jq -r '.token')

curl -H 'Accept: application/json' -H "Authorization: Bearer ${TOKEN}" http://localhost:3210/user_articles?select=title

The first line gets the token from the response and then use the token to GET the result from the second line request.

shrsv commented 1 year ago

A solution to this chaining problem is still in the works; will update here once it's ready.

shrsv commented 1 year ago

Quick update: Started an experimental branch goja, to try to embed a JS processor to enable chaining requests.

Also delegating to a scripting lang, is in line with design philosophy of "delegate to mature tools". By the way, this is pure JS - so we won't allow importing arbitrary npm modules.

Why JS:

  1. Most people working with APIs probably already know JS
  2. Easy to support XPath/JSONPath or even JQ clones right into JS
  3. Even if you don't know any of (2), simple JS object notation + good old loops will take you far
  4. Native support for JSON (and dom manipulation is common too, for xml type responses)
  5. Good amount of power for implementation effort exerted

For simple use case mentioned above in the thread, one barely has to understand JS to process some JSON and store a variable

Some example files with both Requester + Processor (JS) blocks:

Trivial example:

url = "https://google.com"
---
GET
https://${url}

Advanced:

# stage 1

POST
${LOCAL_COORD}/api/login/

{
    "username": "admin",
    "password": "Password@123"
}

---

// filtering, store in var; keyword "let" optional
let TOKEN = result["Token"] 

---

# stage 2
GET
${LOCAL_COORD}/listorgstyle

Authorization: 'Token ${TOKEN}’'

{}

Any early thoughts/comments/suggestions welcome.

PS: Implementation still in progress

shrsv commented 1 year ago

@geohuz - latest release allows chaining requests through Javascript blocks. Please try whenever possible and let me know if any issues.