From fa5a808c458ba1ecacc8b4b12f7be3e118e61cba Mon Sep 17 00:00:00 2001 From: Elias Renman Date: Sat, 27 May 2023 00:58:06 +0200 Subject: [PATCH] feat: added body support --- pico-w/src/decorators.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pico-w/src/decorators.py b/pico-w/src/decorators.py index 849c59e..a1f524f 100644 --- a/pico-w/src/decorators.py +++ b/pico-w/src/decorators.py @@ -39,16 +39,41 @@ class Endpoint: if endpoint != self.path and self.path != '*' or method != self.method: return try: + body = get_body(request) + if body: + kwargs = dict(**kwargs, body=body) + val = function(instance, *args, **kwargs) respond(cl, 200, val) return val except HttpError as e: respond(cl, e.status, e.message) + except Exception as e: + print("Some other unkown error occured:", e) return wrapper +""" +Please note that this only supports content-type 'application/json' +""" + + +def get_body(request: str): + req = request.split("\r\n") + content_type_index = -10 + for index in range(len(req)): + item = req[index] + + if item.find("Content-Type") != -1: + if not item.find("application/json"): + return + content_type_index = index + if (content_type_index + 4) == index: + return item + + class ServerHandler(object): def __init__(self, s: socket.socket): not_found_endpoints = (self.__post_not_found,