picoweb URL Params in routes

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

picoweb URL Params in routes

Post by devnull » Sun Oct 15, 2017 5:55 am

Hi;

I am unable to successfully define a regex to handle url GET parameters such ads:

Code: Select all

http://mydevice/?x=1&y=2&z=3

Code: Select all

def json(req,resp):
  yield from picoweb.jsonify(resp,{"error":True})  

ROUTES = [
  (re.compile("^/\?.*"), json),
  ("/", lambda req, resp: (yield from app.sendfile(resp, path+"index.htm"))),
  (re.compile("^/(.+)"), lambda req, resp: (yield from app.sendfile(resp, path + req.url_match.group(1)))),
]   
I have attempted to look for the path starting with "/?" - what am I doing wrong ??

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: picoweb URL Params in routes

Post by devnull » Sun Oct 15, 2017 7:23 am

OK, I found the reason, the queryParams are already stripped from the url and are available from req.qs

Code: Select all

def json(req,res):
  dict = {x[0] : x[1] for x in [x.split("=") for x in req.qs[1:].split("&") ]}
  print(dict);
  yield from picoweb.jsonify(res,dict) 

davlefou
Posts: 3
Joined: Mon Jan 28, 2019 3:15 pm

Re: picoweb URL Params in routes

Post by davlefou » Mon Jan 28, 2019 3:19 pm

Hi, you have made an mistake, it start with 0:
[code]
dict = {x[0] : x[1] for x in [x.split("=") for x in req.qs[1:].split("&") ]}
[/code]

Post Reply