Created
August 30, 2024 11:30
-
-
Save sashatu/95a29c381239a4d7c13ed3de4a461be0 to your computer and use it in GitHub Desktop.
Simple Node.JS Lambda Function to output the HTTP request parameters in JSON format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const handler = async (event) => { | |
// Extract useful information from the event object | |
const { | |
httpMethod, | |
headers = {}, | |
queryStringParameters = {}, | |
path, | |
body, | |
requestContext: { | |
identity: { sourceIp, userAgent } = {} | |
} = {} | |
} = event; | |
// Convert headers object into an array of key-value pairs for easier readability | |
const headersArray = Object.entries(headers).map(([key, value]) => { | |
return `${key}: ${value}`; | |
}); | |
// Prepare a human-friendly response | |
const response = { | |
statusCode: 200, | |
headers: { | |
"Content-Type": "application/json", | |
}, | |
body: JSON.stringify({ | |
message: "HTTP Request Information V2", | |
method: httpMethod, | |
path: path, | |
headers: headersArray, // Unpacked headers as an array of key-value pairs | |
queryStringParameters: queryStringParameters, | |
body: body, | |
sourceIp: sourceIp, | |
userAgent: userAgent | |
}, null, 2) // Pretty-print JSON with 2-space indentation | |
}; | |
return response; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment