sloev / python-lambdarest

Flask like web framework for AWS Lambda
https://sloev.github.io/python-lambdarest/
MIT License
94 stars 23 forks source link

[BUG] multiValueHeaders not being sent back (ALB) #64

Closed amacks closed 4 years ago

amacks commented 4 years ago

It looks like ALB needs support for multiValueHeaders as part of the return JSON for things like Content-Type, headers does not seem to be properly passed through the ALB. I added a multiValueHeader element by copying and modifying headers in the to_json and inner_lambda_handler and it seems to work as expected and the ALB properly interprets. I can submit a PR, but I cannot vouch for the quality of the code, however it does seem to work

diff --git a/lambdarest/__init__.py b/lambdarest/__init__.py
index d8cd80f..8079e97 100755
--- a/lambdarest/__init__.py
+++ b/lambdarest/__init__.py
@@ -22,10 +22,11 @@ class Response(object):
     if no headers are specified, empty dict is returned
     """

-    def __init__(self, body=None, status_code=None, headers=None):
+    def __init__(self, body=None, status_code=None, headers=None, multiValueHeaders=None):
         self.body = body
         self.status_code = status_code
         self.headers = headers
+        self.multiValueHeaders = multiValueHeaders
         self.status_code_description = None
         self.isBase64_encoded = False

@@ -42,8 +43,12 @@ class Response(object):
             if do_json_dumps
             else self.body,
             "statusCode": status_code,
-            "headers": self.headers or {},
         }
+        ## handle multiValueHeaders if defined, default to headers
+        if (self.multiValueHeaders == None) :
+            response["headers"] = self.headers or {}
+        else:
+            response["multiValueHeaders"] = self.multiValueHeaders
         # if body is None, remove the key
         if response.get("body") == None:
             response.pop("body")
@@ -235,21 +240,22 @@ def create_lambda_handler(
                             raise ValueError("Response tuple has more than 3 items")

                         # Unpack the tuple, missing items will be defaulted
-                        body, status_code, headers = response + (None,) * (
-                            3 - response_len
+                        body, status_code, headers, multiValueHeaders = response + (None,) * (
+                            4 - response_len
                         )

                     elif isinstance(response, dict) and all(
-                        key in ["body", "statusCode", "headers"]
+                        key in ["body", "statusCode", "headers", "multiValueHeaders"]
                         for key in response.keys()
                     ):
                         body = response.get("body")
                         status_code = response.get("statusCode") or status_code
                         headers = response.get("headers") or headers
+                        multiValueHeaders = response.get("multiValueHeaders") or multiValueHeaders

                     else:  # if response is string, int, etc.
                         body = response
-                    response = Response(body, status_code, headers)
+                    response = Response(body, status_code, headers, multiValueHeaders)
                 return response.to_json(
                     encoder=json_encoder,
                     application_load_balancer=application_load_balancer,
sloev commented 4 years ago

Looking tonite, expect feedback within next days. Thx and take care!

tor. 30. apr. 2020 16.40 skrev Aaron Macks notifications@github.com:

Assigned #64 https://github.com/trustpilot/python-lambdarest/issues/64 to @sloev https://github.com/sloev.

— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub https://github.com/trustpilot/python-lambdarest/issues/64#event-3289775023, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGVGUOF3KHRM4KXRPLPP63RPGEUZANCNFSM4MVUAFBA .

sloev commented 4 years ago

there is not enough for me here to work on, sorry.

i would like

If you have time to supply any of that in a pr i will take time to verify and get it in. If not i will not have the time right now to do the journey myself, sorry :-)

amacks commented 4 years ago

ok, I will put together a PR in the next day or 3

sloev commented 4 years ago

Thx

ons. 6. maj 2020 16.22 skrev Aaron Macks notifications@github.com:

ok, I will put together a PR in the next day or 3

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/trustpilot/python-lambdarest/issues/64#issuecomment-624673841, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGVGUJRGBRK3BMYUKQYNELRQFXBFANCNFSM4MVUAFBA .

sloev commented 4 years ago

fixed in #65