Will this function send the same request headers in the response?
Maybe it’s missing this header: Content-Type:application/json
Thanks @dipro , I’m pretty new in it, could you copy/paste my code and put your proposal in proper place? Thank you in advance!
@Artem
return {
'statusCode': 200,
'body': json.dumps(response_message, ensure_ascii=False)
}
should be changed to:
return {
'statusCode': 200,
'body': json.dumps(response_message, ensure_ascii=False),
'headers': {
'Content-Type': 'application/json'
}
}
From the documentation for lambda function URLs, here is the full structure of the responses. No fields are required, and returning void returns a 200 with no body. Obviously not what you want here, but could be of use in some situations.
{
"statusCode": 201,
"headers": {
"Content-Type": "application/json",
"My-Custom-Header": "Custom Value"
},
"body": "{ \\"message\\": \\"Hello, world!\\" }",
"cookies": [
"Cookie_1=Value1; Expires=21 Oct 2021 07:48 GMT",
"Cookie_2=Value2; Max-Age=78000"
],
"isBase64Encoded": false
}
Lambda infers the response format for you. If your function returns valid JSON and doesn’t return a statusCode
, Lambda assumes the following:
statusCode
is 200
.
content-type
is application/json
.
body
is the function response.
isBase64Encoded
is false
.
What this means is if you want to return a 200
as application/json
then all you really need is:
return json.dumps(message_response) //try it without the json.dumps and see what happens.
Lambda will interpret it as:
return {
"statusCode": 200,
"body": {
"challenge" : "3eZbrw1aBm2rZgRNFdxV2595E9CY3gmdALWMmHkvFXO7tYXAYM8P"
},
'headers': {
'Content-Type': 'application/json'
}
}
Nice response! Thanks for jumping in @anon29275264
Thank you Cody. Lambda return as you told me. But when I got to the monday.com - webhooks - choose one - put Lambda URL (public accessible for the tests) - getting error "Failed to communicate "
Uploading: Screenshot 2023-09-21 at 10.06.07.png…
Response
{
"statusCode": 200,
"body": "{\\"challenge\\": \\"3eZbrw1aBm2rZgRNFdxV2595E9CY3gmdALWMmHkvFXO7tYXAYM8P\\"}",
"headers": {
"Content-Type": "application/json"
}
}
Function Logs
START RequestId: e2d8eab1-fbbf-43ec-8a02-8f7cdc88a4ad Version: $LATEST
Received event: {
"challenge": "3eZbrw1aBm2rZgRNFdxV2595E9CY3gmdALWMmHkvFXO7tYXAYM8P"
}
Response message: {
"challenge": "3eZbrw1aBm2rZgRNFdxV2595E9CY3gmdALWMmHkvFXO7tYXAYM8P"
}
END
Lambda function:
iimport json
def lambda_handler(event, context):
# Print the entire 'event' object for debugging with double quotes
print("Received event:", json.dumps(event, indent=2))
# Extract the challenge value from the webhook event data
challenge = eventn'challenge']
# Create a dictionary with the challenge message using the extracted challenge value
response_message = {
"challenge": challenge
}
# Print the response message with double quotes
print("Response message:", json.dumps(response_message, indent=2, ensure_ascii=False))
return {
'statusCode': 200,
'body': json.dumps(response_message, ensure_ascii=False),
'headers': {
'Content-Type': 'application/json'
}
}
I know what the issue is, but I’m not going to directly tell you the answer because its learning time!
You’re testing your code with the “test” feature of the lambda console, correct? I suggest you check the cloudwatch logs when you test with the actual webhook. You may see the issue in your received event. Here is how to access them:
Look at the spoiler below for more once you check your logs!
Another hint is that function URL events are API Gateway Http API / apigateway-http-api-proxy events. In the console test setup, there are optional templates, you may want to check those out to get an idea of how different source’s events are structured.
I’d highly suggest you check out this project if you’re going to be doing python lambda functions regularly. Particularly look at Event Source Data Classes. In this case you want APIGatewayProxyEventV2. But you’ll want to look into AWS SAM for dealing with the dependencies better.
docs.powertools.aws.dev