Just moozing

Before you can check your notes, you must make them…

RESTful API in python

with one comment

logoCreating a RESTful API has long been on my todo list, and now I created one. I used python and flask for it.

RESTful APIs are, in the way I use it, a stateless way of retrieving or setting data using the HTTP protocol. So instead of sending html, we send JSON data both ways.

Building the API

First try was to use python eve, which is a library build on top of flask to make RESTful interfaces. Is was very easy to use, but it was designed to use a database backend, and retrieve and store data in either mongodb. I needed to be able to trigger more python code, not just save data.

I ended up using flask, and made a simple RESTful API in 41 lines. It is now on github. This was a good starting point for me.

It defines three paths

  • / – returns a message
  • /gpios – returns a list of gpios defined
  • /gpios/ – return the value of the gpio or sets the value.

Note that gpio is just a name for now. It is implemented as a dictionary.

When working with RESTful APIs, you use GET, PUT, POST, DELETE and the other HTTP command words. It is also very readable in wireshark.

Connecting (the easy way)

You can access the API using python or curl (as described here). So e.g. setting the gpio A0 to 10, may be done like this

$ curl http://localhost:5000/gpio/A0 -X PUT -d '{"data": "10"}' -H "Content-type: application/json"

or, in pyhon

>>> from requests import put
>>> put('http://localhost:5000/gpio/A0', json={'data': '10'})
<Response [204]>

easy, but people often use HTML and javascript for these things…..


Connecting (the hard way)

Admittedly, my HTML and javascript skills are rusty, but I still expected it to be very little in terms of code.

I ended up with an HTML page (including javascript) in 77 lines. Working with javascript, just makes me like python even more. The code is here.

There were issues related to synchronous vs. asynchronous XMLHttpRequests. More on that here and here. Modern browser have disabled synchronous requests “in the main GUI thread”.

Besides my  strained relationship with javascript and HTML, I am pleased with the result as an example of how to access RESTful APIs from the browser.

End notes

  • I want to use this to make an API on my raspberry pi to access the IO ports.
  • There are some RESTful API design best practice recommendation that I have not followed, like the part about enabling accessing specific versions of the API. More on that here or here.
  • Check out postman if you are using Chrome.
  • Full rest example repo is here.


Written by moozing

August 9, 2016 at 12:00

Posted in Tech

Tagged with , ,

One Response

Subscribe to comments with RSS.

  1. […] weekend, I created my first android app. It was a native java app, that connected to my RESTful API […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: