Saturday, April 11, 2020

emacs as rest client (API testing interface, like postman)

I love being able to use emacs as a REST API testing interface, kinda like postman, but in emacs.

Here are two ways to do this:

- restclient-mode http://emacsrocks.com/e15.html
- walkman https://github.com/abrochard/walkman

In short

Restclient should be your thing 90% of the time.
walkman prob 10%. But walkman works inside org-mode, so if you like org,
maybe can be 100% your thing.

In long


Restclient's thing even has a cute video and everything. So,
noob-friendly.
Resclient is probably the de facto standard right now to use in this
kind of thing though.
It's awesome, you can easily use variables, which can be plain elisp,
and it uses emacs' internal http request libraries for HTTP interaction.
The output is json-prettified and all, and requests are done async, very
nice.

The only thing, is that those libraries appear to be a bit buggy... and
sometimes won't work as expected.
I have this thing, that for this specific site, I always get a 404 or
500 status code.
But when I generate the curl request from the exact same block, it works
(curl works, elisp's request library apparently doesn't like my
headers ?).

So for that API, I use walkman inside org-mode, which uses pure curl,
and that works perfectly.

I like walkman a lot, as I love org-mode.
But the variable definitions must done in elisp, and you have to quote
them to use them... which is not very readable (and elisp code doesn't
look too good in org mode).

Samples

restclient

# -*- mode: restclient; -*-
# block1 gets fsf's site
GET https://www.fsf.org

# press C-c C-c somewhere in this block to send the request
# block2 POSTing stuff with a payload
POST https://your-fav-api/v1/resource
YOUR-HEADERS-HERE
{your payload
can use multiple lines
}

# block3 ...

walkman


* get a page
GET https://www.fsf.org

# you do C-c C-RET here to send the request

* post something
  POST https://your-favorite-api/v1/resource
  - HEADER1
  - HEADER2
  { PAYLOAD-HERE }

That's about it. They're both pretty intuitive, and the documentation is
clear enough to let you do whatever you want within like 5mins of
looking for it.

I'd usually want to test/use the API interface in a different file. So I
guess I'd be using restclient mainly, linking it from my "task" file in
org-mode. Mostly because the variables, and output are more readable
(highlighted and everything).
And when that fails, use walkman.

But that's just me.

No comments:

Post a Comment