Page 1 of 1

ESP8266 + RESTful API (GET/POST)

Posted: Fri Mar 08, 2019 11:32 pm
by jpinzon408
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.

Re: ESP8266 + RESTful API (GET/POST)

Posted: Fri Mar 22, 2019 10:41 am
by cefn
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.

Re: ESP8266 + RESTful API (GET/POST)

Posted: Fri Mar 22, 2019 2:10 pm
by rsnyman
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

Re: ESP8266 + RESTful API (GET/POST)

Posted: Fri Aug 06, 2021 7:20 pm
by JohnnyGC
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)