ESP8266 + RESTful API (GET/POST)

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
jpinzon408
Posts: 1
Joined: Fri Mar 08, 2019 11:17 pm

ESP8266 + RESTful API (GET/POST)

Post by jpinzon408 » Fri Mar 08, 2019 11:32 pm

Hi, I'm interested in knowing if they have a restful code API that works for the ESP8266 and has the characteristics of GET, PUT, POST and DELETE data, which the python code is listening to the methods and can execute an action.

cefn
Posts: 230
Joined: Tue Aug 09, 2016 10:58 am

Re: ESP8266 + RESTful API (GET/POST)

Post by cefn » Fri Mar 22, 2019 10:41 am

The simple HTTP server at https://docs.micropython.org/en/latest/ ... k_tcp.html can be modified to inspect the 'method' value passed in a HTTP header, and call different functions depending on what method is found.

rsnyman
Posts: 1
Joined: Fri Mar 22, 2019 6:32 am

Re: ESP8266 + RESTful API (GET/POST)

Post by rsnyman » Fri Mar 22, 2019 2:10 pm

Needed a web server that I could use on my ESP8266, but Picoweb by Paul Sokolovsky (see https://github.com/pfalcon/picoweb ) was still too big for the limited RAM of the ESP8266, so I stripped it down even further. It works like Flask (see http://flask.pocoo.org/ ).

Here's my version: https://gitlab.com/superfly/dawndoor/bl ... oor/web.py
And here's how I use it: https://gitlab.com/superfly/dawndoor/bl ... oor/app.py

My code is under the MIT license, as is Picoweb

JohnnyGC
Posts: 3
Joined: Sun Jul 25, 2021 9:32 pm

Re: ESP8266 + RESTful API (GET/POST)

Post by JohnnyGC » Fri Aug 06, 2021 7:20 pm

Great work @rsnyman

I'm quite a noob at micropython and web servers and I'm trying to implement a Restful server on an ESP32 using 'web.py' before migrating it to ESP8226. The client in this instance is Home Assistant and is using the following configuration:

Code: Select all

switch:
  - platform: rest
    resource: "http://192.168.1.101:8118/switch"
    name: "Rest Switch"
    method: "post"
    body_on: '{"active": "true"}'
    body_off: '{"active": "false"}'
    headers:
      Content-Type: application/json
Below is the app code I'm using. The 'elif' clause was added because 'request.form' is returning {"{'active': 'true'}": True} instead of {'active': 'true'} which is returned as expected by tinyweb or W10's Invoke-RestMethod. Whilst this works it seems like an ugly solution that won't be easy in other use cases. Any clues as to what is causing this would be appreciated.

Code: Select all

@webapp.route('/switch', method='POST')
def post_switch(request, response):
    yield from request.read_form_data()
    sw = 0
    if request.form.get('active'):
        if request.form['active'] == 'true':
            sw = 1
    elif request.form.get('{"active": "true"}'):
            sw = 1
    switch.value(sw)
    if sw:
        data = {'active': 'true'}
    else:
        data = {'active': 'false'}
    yield from jsonify(response, data)

Post Reply