From ee7f90a0a5efb8ae6486170e0f20f01f38c6acac Mon Sep 17 00:00:00 2001 From: dcore94 Date: Mon, 17 Jul 2023 17:31:13 +0200 Subject: [PATCH] added support for multipart/form-data with adherence to mime type specification for single parts --- pyexecplugins/Http.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pyexecplugins/Http.py b/pyexecplugins/Http.py index 1a86b05..ae629df 100644 --- a/pyexecplugins/Http.py +++ b/pyexecplugins/Http.py @@ -31,10 +31,30 @@ class Plugin(PyExecPlugin): self.readtimeout = data.get("read-timeout") or 10 self.notimeout = (data.get("no-timeout") in (True, "true", "True", "TRUE", 1, "1", "y", "Y", "yes", "YES")) or False + def adaptMultipart(self, parts): + req = {} + for p in parts: + part = parts[p] + if(type(part) == str): + req[p] = part + elif (type(part) == list and len(part) > 0 and len(part) <= 2): + if len(part) == 1: + req[p] = (p, part[0]) + elif len(part) == 2 and part[1].find("json"): + req[p] = (p, json.dumps(part[0]), part[1]) + else: + req[p] = (p, part[0], part[1]) + else: + raise Exception("Unable to parse multipart request. For every key either single string or an array matching requests tuple format is expected. Got %s with length %d for key %s" % (type(part), len(part), p)) + return req + def doRequest(self): #logging.getLogger("pyexec").debug("%s - %s - %s - %s",self.method, self.url, self.contenttype, self.accept) if self.contenttype != None and self.contenttype.find("json") != -1: self.request = requests.Request(self.method, self.url, headers=self.headers, json = self.body) + if self.contenttype != None and self.contenttype.find("multipart/form-data") != -1: + self.headers.pop("content-type") + self.request = requests.Request(self.method, self.url, headers=self.headers, files = self.adaptMultipart(self.body)) elif self.contenttype != None and self.contenttype.find("text") != -1: self.request = requests.Request(self.method, self.url, headers=self.headers, data = self.body.encode('utf-8')) else: