Restclient (Postman for Emacs)
October 22, 2017
Hi everyone.
Here is my notes about restclient
that can help you to forget about Postman
(which is cool as well).
Define the action
Here is the example:
#
# get all people from star wars
#
GET https://swapi.co/api/people
To define an action you need to start with a comment.
Then you need to set method (GET, POST, PUT and other) and the url of course.
Now you can execute the query. Press C-c C-c
(CTRL+c twice) and new buffer (*HTTP Response*
) with the result will be opened.
At the bottom of that buffer you can see response headers:
// GET https://swapi.co/api/people
// HTTP/1.1 200 OK
// Date: Sun, 22 Oct 2017 19:03:17 GMT
// Content-Type: application/json
// Transfer-Encoding: chunked
// Connection: keep-alive
// Allow: GET, HEAD, OPTIONS
// Vary: Accept, Cookie
// X-Frame-Options: SAMEORIGIN
// Etag: "d080408a4d656729b0f52a6feb0783ca"
// Via: 1.1 vegur
// Server: cloudflare-nginx
// CF-RAY: 3b1eb73cda7d63af-FRA
// Request duration: 1.663359s
Pretty simple, ha?
Set headers
To define headers you need to write them just below the URL definition:
#
# get all people from star wars
#
GET https://swapi.co/api/people
User-Agent: Emacs Restclient
Content-Type: application/json
By pressing C-c C-c
the headers will be attached to the request.
Post payload
If you want to send data to the server you can define it right after empty line of the URL (or headers if exist) definition.
Here is an example from restclient.el
repo.
#
# Post works too, entity just goes after an empty line. Same is for PUT.
#
POST https://jira.atlassian.com/rest/api/2/search
Content-Type: application/json
{
"jql": "project = HSP",
"startAt": 0,
"maxResults": 15,
"fields": [
"summary",
"status",
"assignee"
]
}
What if I want to post a file?
It’s also possible and it’s pretty easy
#
# post a file
#
POST http://httpbin.org/post
Content-Type: text/plain
< /etc/hosts
You can even define variables
Variables are useful to define the host or test user credentials.
Here how you can define it.
:myvar = https://google.com
You can also use elisp to set a value
:myvar := (some (artbitrary 'elisp)
use :variable-name
syntax to get the value of variable:
#
# use a variable
#
GET :myvar/imghp
Restclient.el is a great package you definitely should try. It allows you quickly explore or test the API without leaving the Emacs.
Try it <3
Feel free to contact me for feedback or questions. Find my contacts on About page.