Bootstrap FreeKB - Amazon Web Services (AWS) - POST JSON to an API Gateway Lambda Function using cURL
Amazon Web Services (AWS) - POST JSON to an API Gateway Lambda Function using cURL


There are various way to POST JSON to one of your AWS Lambda Functions.

Let's say you have created an API Gateway that contains a HTTP POST route that will forward a POST request onto a Lambda Function, perhaps something like this.

 

Let's say you have a Lambda Function, perhaps something like this (Python in this example). This is just a super simple example with no error handling to highlight the most relevant markup. In this example, the Lambda Function expects the input JSON to pass in the foo and bar keys.

import json

def lambda_handler(event, context):
    print(event)

    json_loads_event_body = json.loads(event['body'])

    foo = json_loads_event_body['foo']
    bar = json_loads_event_body['bar']
       
    return {
        'statusCode': 200,
        'body': json.dumps({
            "result": "success",
            "foo": json_loads_event_body['foo'],
            "bar": json_loads_event_body['bar']
        })
    }       

 

Here is how you could POST using curl.

~]$ curl --request POST --url https://abcdefg123.execute-api.us-east-1.amazonaws.com/postSendgrid --header 'content-type: application/json' --data '{"foo":"Hello","bar":"World"}'
{"result": "success", "foo": "Hello", "bar": "World"}

 

Notice also in this example that the Lambda Function includes print(event). This is super helpful when testing/debugging, but you probably wouldn't want to include print(event) after your testing/debugging is done, as this could add a bit of additional cost each time your Lambda Function is called.

If you have print(event), then Cloudwatch > Log groups > /aws/lambda/myapi > yyyy/mm/dd/[$LATEST]abcdefg123456789abdefg123456789 should have something like this. For more details on this, check out my article Amazon Web Services (AWS) - API Gateway Lambda Function and Cloudwatch Logs.

{
   "version":"2.0",
   "routeKey":"POST /postSendgrid",
   "rawPath":"/postSendgrid",
   "rawQueryString":"",
   "headers":{
      "accept":"*/*",
      "content-length":"15",
      "content-type":"application/json",
      "host":"abcdefg.execute-api.us-east-1.amazonaws.com",
      "user-agent":"curl/8.0.1",
      "x-amzn-trace-id":"Root=1-65712b4e-32aaec9937d2924457e3df71",
      "x-forwarded-for":"10.11.12.13",
      "x-forwarded-port":"443",
      "x-forwarded-proto":"https"
   },
   "requestContext":{
      "accountId":"123456789012",
      "apiId":"abcdefg",
      "domainName":"abcdefg.execute-api.us-east-1.amazonaws.com",
      "domainPrefix":"abcdefg",
      "http":{
         "method":"POST",
         "path":"/postSendgrid",
         "protocol":"HTTP/1.1",
         "sourceIp":"10.11.12.13",
         "userAgent":"curl/8.0.1"
      },
      "requestId":"PjO0Ujb2IAMEJ9A=",
      "routeKey":"POST /postSendgrid",
      "stage":"$default",
      "time":"07/Dec/2023:02:17:50 +0000",
      "timeEpoch":1701915470546
   },
   "body":"{\"result\":\"success\", \"foo\":\"Hello\", \"bar\":\"World\"}",
   "isBase64Encoded":false
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 2ca5c4 in the box below so that we can be sure you are a human.