0

So I have a weird issue with AWS Lambda. I've got an endpoint that I am POST'ing to with a cookie, but the event JSON for some reason is invalid as it contains single quotes instead of JSON spec double quotes, which in turn causes an exception with body = json.loads(event['body']) so I've resorted to using ast for input parsing:

def lambda_handler(event, context):

    body = ast.literal_eval(str(event)['body'])

which is throwing an exception:

[ERROR] TypeError: string indices must be integers
Traceback (most recent call last):
  File "/var/task/app.py", line 23, in lambda_handler
    body = ast.literal_eval(str(event)['body'])

Here is what the input event looks like from CloudWatch:

2021-05-23T02:22:33.437+00:00
{'version': '2.0', 'routeKey': 'POST /core', 'rawPath': '/Prod/core', 'rawQueryString': '', 'headers': {'accept-encoding': 'gzip', 'content-length': '994', 'content-type': 'application/json; charset=UTF-8', 'host': 'mrmyi8psq3.execute-api.us-east-2.amazonaws.com', 'user-agent': 'Dart/2.10 (dart:io)', 'x-amzn-trace-id': 'Root=1-60a9bc68-638af3f87db7635e129612a0', 'x-forwarded-for': '106.204.193.37', 'x-forwarded-port': '443', 'x-forwarded-proto': 'https'}, 'requestContext': {'accountId': '246891114792', 'apiId': 'mrmyi8psq3', 'domainName': 'mrmyi8psq3.execute-api.us-east-2.amazonaws.com', 'domainPrefix': 'mrmyi8psq3', 'http': {'method': 'POST', 'path': '/Prod/core', 'protocol': 'HTTP/1.1', 'sourceIp': '106.204.193.37', 'userAgent': 'Dart/2.10 (dart:io)'}, 'requestId': 'fwpgajclCYcEMtg=', 'routeKey': 'POST /core', 'stage': 'Prod', 'time': '23/May/2021:02:22:32 +0000', 'timeEpoch': 1621736552901}, 'body': '{"message":"test message","cookie":"eyJraWQiOiJOVlwvSXB5V0xGblRvTXBVaHhqTUVDUXQ3UWFxOVNxcVUxVkFIa2p4eFwvT3M9IiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiJlNTljZGZhYS05OWY5LTQ3OTAtYWRkZi1lOWM4YTgwMTE1ZWMiLCJhdWFtYXpvbmF3cy5jb21cL3VzLWVhc3QtMl91dGlsM2hYN0kiLCJjb2duaXRvOnVzZXJuYW1lIjoiZTU5Y2RmYWEtOTlmOS00NzkwLWFkZGYtZTljOGE4MDExNWVjIiwiZXhwIjoxNjIxNzQwMTIxLCJpYXQiOjE2MjE3MzY1MjEsImVtYWlsIjoicGF2YW55YWRhdkBob3RtYWlsLmNvbSJ9.kK6NRpgiLNmgrzuB91APy15fvJfDt2FtmZQKvOLQJAK8J522OH9hiD4Sroh_F_DivYO98exseEV99FVuyiAB7I70QpYeZUy2M0OF_VSt9AZ8cM_XUN8gf1NHNQoIJlsRNOcok6hJn4Bp7mYhZuyQmzpFk1Hq9joMcjvHBXa0iGBS6stp-2bVH23yVrrYYeDl0lyUfVLUcwwT3BP7jZDcVDcWpfqZYNR5yU4o7Tw0oZKESNCxIr3Hh1EZGMUJwhb-RsSKb9nMafQYJQ-VtFNRj5rlJGf_M1QYwvli-g82p7C9Kf6SbgsjDYKxsjdxZAyouVaXnaL6QgAOLCEMxopTLA"}', 'isBase64Encoded': False}

Any thoughts would be greatly appreciated!

1
  • The problem is that you are using str(event) so a string representation for event is returned and strings dont have a index ['body'] but us can use integer indicies on them. I think Jose Romeros answer should solve your problem Commented May 23, 2021 at 2:55

1 Answer 1

2

I think the issue is that you're evaluating the string rather than the event with the index. So instead of

str(event)['body']

it should be

str(event['body'])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.