Lambda function URL ERRO when trying to pass data to it in a browser

0

I have a function accessing DynamoDB data. It works perfectly when I hardcode what I wish to scan for, even via a function URL executing from a browser.

Now, instead of hardcoding, I need to pass a piece of data to this function. However first, I tested the Python code via a test event JSON adding this to it: "Username": "Elvis". At the beginning of the handler in Python, I add:

  • myUsername = event.get("Username", {})
  • print(Username) This also worked perfectly. In Lambda function executes with this test data.

Now, when I delete the test event with the JSON data, and then add to the Lambda URL @ the end of the line:?Username=Elvis, the browser message is: Internal Server Error.

If I retrace my steps by adding back the JSON data of: "Username": "Elvis", and then run the URL as AWS gave it to me when I created the function URL, the return code again is exactly as I expected it.

Also, At the moment to test this, the Auth type is None. I will add the Auth type when I have the basic features working.

I'm stumped.

Any help?

profile picture
Petrus
asked 11 days ago121 views
2 Answers
0

Hello.

If you look at the Lambda log, the element "queryStringParameters" contains parameters.
Therefore, you can check the "Username" by getting the parameters with "event.get('queryStringParameters')" as shown below.
https://docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html

import json

def lambda_handler(event, context):
    
    param = event.get('queryStringParameters')
    print(param['Username'])

If you include the request body, it will be included in an element called "body", so you can retrieve it as follows.

import json

def lambda_handler(event, context):
    
    print(event)
    param = event.get('queryStringParameters')
    print(param['Username'])
    body = event.get('body')
    print(json.loads(body)['Username'])

A sample request that includes a request body looks like this:

curl 'https://test.lambda-url.ap-northeast-1.on.aws/?Username=Elvis' -H 'content-type: application/json' -d '{ "Username": "test" }'
profile picture
EXPERT
answered 11 days ago
profile pictureAWS
EXPERT
reviewed 11 days ago
0

Thank you. Best answer I could not find anywhere online. You're the best!

profile picture
Petrus
answered 10 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions