Query String Parameters with Lambda function URL

1

I have a Lambda function with URL and I am trying to POST some parameters to it with curl:

curl -d "item=one"  htpps://<lambda-url>

The queryStringParameters field of event is empty. Here's the function:

import json

def lambda_handler(event, context):
    if "queryStringParameters" in event:
        return
        {
        'statusCode': 200,
        'body': json.dumps(event["queryStringParameters"])
    }
    return {
        'statusCode': 200,
        'headers': {
            'Content-Type': 'text/html'
        },
        'body': 'Empty'
    }

The output of curl is null. I inspected the event object and the parameters do not appear anywhere in it.

What am I doing wrong and how can I pass query string parameters, such as a form from a website, to a URL Lambda using POST?

Romanko
posta un anno fa9862 visualizzazioni
2 Risposte
0

-d is used to sent a payload, not query parameters.

What you need to do is: curl https://<lambda-url>/item=one

profile pictureAWS
ESPERTO
Uri
con risposta un anno fa
  • Hi Uri, Thank you for the reply. I tried: curl -X POST https://<lambda-id>.lambda-url.ap-southeast-2.on.aws/?item=two The result is null as well. This beats me. I created a test event:

    {"queryStringParameters":
    {
      "key1": "value1",
      "key2": "value2",
      "key3": "value3"
    }
    }
    

    I also added a print statement to the Lambda function:

    import json
    
    
    def lambda_handler(event, context):
        if "queryStringParameters" in event:
            print(event["queryStringParameters"])
            return
            {
            'statusCode': 200,
            'body': json.dumps(event["queryStringParameters"])
        }
        return {
            'statusCode': 200,
            'headers': {
                'Content-Type': 'text/html'
            },
            'body': 'Empty'
        }
    

    The execution results are:

    Test Event Name
    query-string-params
    
    Response
    null
    
    Function Logs
    START RequestId: 524dfae5-72de-4170-99bd-bc7bbcb18edf Version: $LATEST
    {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
    ...
    

    Any further help would be much appreciated.

0

Anwering my own question.

    return {
             'statusCode': 200}

and

     return
       { 
           `statusCode`: 200 }

are not equivalent. The first returns the object, while the second returns None (which somehow becomes null). I miss FORTRAN. Any plans of adding it to Lamdba runtimes?

Romanko
con risposta un anno fa

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande