Page 1 of 1

picoweb URL Params in routes

Posted: Sun Oct 15, 2017 5:55 am
by devnull
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 ??

Re: picoweb URL Params in routes

Posted: Sun Oct 15, 2017 7:23 am
by devnull
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) 

Re: picoweb URL Params in routes

Posted: Mon Jan 28, 2019 3:19 pm
by davlefou
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]