0

I have a basic lambda function which accepts a JSON, and transforms to XML and uploads the same to s3 bucket. I am running the test through the lambda console and the xml is getting generated and uploaded to s3 as well. Issue happens when I run the test through API Gateway, and there I get the error:

"message": "Internal server error" and Status: 502. The error also points out where is the code issue:
{"errorMessage": "'header'", "errorType": "KeyError", "stackTrace": ["  File \"/var/task/sample.py\", line 21, in printMessage\n    b1.text = event['header']['echoToken']\n"]}

The same code is working when running through lambda console. Below is the code snippet with the line that throws the error:

def printMessage(event, context):
    print(event)
    #event = json.loads(event['header'])
    root = ET.Element("Reservation")
    m1 = ET.Element("header")
    root.append(m1)
    b1 = ET.SubElement(m1, "echoToken")
    b1.text = event['header']['echoToken'] #this line throws error

I got a similar question here: AWS Lambda-API gateway "message": "Internal server error" (502 Bad Gateway). Below is the print of event object.

{'header': {'echoToken': '907f44fc-6b51-4237-8018-8a840fd87f04', 'timestamp': '2018-03-07 20:59:575Z'}, 'reservation': {'hotel': {'uuid': '3_c5f3c903-c43d-4967-88d1-79ae81d00fcb', 'code': 'TASK1', 'offset': '+06:00'}, 'reservationId': 12345, 'confirmationNumbers': [{'confirmationNumber': '12345', 'source': 'ENCORA', 'guest': 'Arturo Vargas'}, {'confirmationNumber': '67890', 'source': 'NEARSOFT', 'guest': 'Carlos Hernández'}], 'lastUpdateTimestamp': '2018-03-07 20:59:541Z', 'lastUpdateOperatorId': 'task.user'}}

As per the solution in the link, if I try to do : header = json.loads(event['header']) I get the below error:

"errorMessage": "the JSON object must be str, bytes or bytearray, not dict",

This is a task that I need to complete, and I have tried few things, but is always failing when tested through API Gateway and POSTMAN. So, my question is: I don't want to change my complete code for XML transformation, and is there any way that I load the complete event object in python, so that it works both through lambda console and API gateway?

Thanks in Advance!!

Abhinash Jha
  • 165
  • 1
  • 3
  • 17

1 Answers1

1

The problem is similar to the one discussed here

Below code will fix the line causing the issue.

eventDict = json.loads(event)
b1Text = json.dumps(eventDict['header']['echoToken'])

print(b1Text)
dossani
  • 1,892
  • 3
  • 14
  • 23
  • I did try the above options but it gives sn error when i run in lambda console. Below is teh error: "errorMessage": "the JSON object must be str, bytes or bytearray, not dict", – Abhinash Jha May 01 '21 at 04:31
  • I checked the shared url, and made the changes accordingly. Below is the change I did: eventDict = json.dumps(event) event = json.loads(eventDict) I am using the using the event again , so that I can use it as a dictionary to get the values that I need to create the XML. Again, in AWS lambda the XML is generated fine, and the same is uploaded to S3 bucket, But when I test through API Gateway, it causes the same error :"errorType": "KeyError", "stackTrace": [" File \"/var/task/sample.py\", line 26, in printMessage\n b1.text = event['header']['echoToken'] – Abhinash Jha May 02 '21 at 05:04
  • I am passing my JSON in the request body section in AWS Gateway. – Abhinash Jha May 02 '21 at 05:09
  • the answer put me in right direction. marking as right. thanks – Abhinash Jha May 05 '21 at 21:32