EMACS REST Client for Python Web API Testing

(Last Updated On: September 22, 2018)

In previous posts, I covered writing a Python REST API.  In this article, we will test our API from within EMACS!  We are going to install an EMACS rest client to do our testing.  This will cut down your development time because you can do everything from one IDE (EMACS) and not have to switch to a browser or a another web client to test your REST API.

Installing restclient.el the EMACS Rest Client

Load up emacs and run the following command to install restclient.el:

M-x package-install RET restclient RET

Now that we have it installed we can use it to test a REST API.

Testing REST APIs

Clone our sample REST API written in Python using Tornado:

https://github.com/admintome/tornado_example/tree/post.

CD to the directory and checkout the ‘post’ branch.  Activate your python environment.  For me this is pyenv.

$ pyenv activate tornado

Make sure you have all the requirements installed:

(tornado) $ pip installl -r requirements.txt

Startup emacs and load the test.txt file.

(tornado) $ emacs test.txt

Here is the contents of test.txt:

# Test the mainhandler
GET http://localhost:8888/v1
# Test the add book handler GET
GET http://localhost:8888/v1/addbook?title=test2&author=bill
# Test the add book handler POST
POST http://localhost:8888/v1/addbook
Content-Type: application/json

{
 "title": "test",
 "author": "bill"
}
# Test the show all books handler
GET http://localhost:8888/v1/getbooks
# Test the del book handler GET
GET http://localhost:8888/v1/delbook?title=test
# Test the del book handler POST
POST http://localhost:8888/v1/delbook
Content-Type: application/json

{
 "title": "test2"
}

This is a basic text file that will serve as commands to EMACS Rest Client.  Commands are separated by the ‘#’ character which also serves as comments.  Our first command demonstrates how to send simple GET requests.  The next command shows how to send a GET request that has parameters that will translate to variables in our python web API.

The third command demonstrates how to use POST method and send variables in JSON form.

Emacs rest client in action
Emacs rest client

Running REST API Queries

Start the restclient mode with this command:

M-x restclient-mode RET

Next put the cursor on the line you want to run and press:

C-c C-v

This will run the command on the line you are on and show the results in another window.  You can repeat this to test other endpoints of your API.  It helped me when doing this to have the source I was working in another window:

C-x 3

C-x D find the source file you are working on then hit RET

Now as you test you can change your code if it isn’t working as expected.

This is very powerful when you want to test your code by performing actual calls to your API.  Below is a video I made of this in action.

For more of my articles on EMACS click here.

Need more Python training?

Checkout these awesome Python courses on Pluralsight.  Pluralsight has great video training courses at a great price.  I use Pluralsight every time I need to learn something new.

Conclusion

I hope you have enjoyed this article, if so please leave a comment below.  For more articles, please signup for the AdminTome Blog below.  Also please feel free to share the article to your friends using the buttons to the left.  Thanks again for reading this post.

 

Leave a Comment

you're currently offline