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
질문됨 일 년 전256회 조회
2개 답변
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
전문가
답변함 일 년 전
profile pictureAWS
전문가
검토됨 일 년 전
0

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

profile picture
답변함 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠