- Le plus récent
- Le plus de votes
- La plupart des commentaires
I was able to find a solution for this.
I found it on stackoverflow, here is the link to the answer https://stackoverflow.com/a/71195539
Below is the Template
## Velocity Template used for API Gateway request mapping template
## "@@" is used here as a placeholder for '"' to avoid using escape characters.
#set($includeHeaders = true)
#set($includeQueryString = true)
#set($includePath = true)
#set($requestContext = "{@@account-id@@:@@$context.identity.accountId@@,@@api-id@@:@@$context.apiId@@,@@api-key@@:@@$context.identity.apiKey@@,@@authorizer-principal-id@@: @@$context.authorizer.principalId@@,@@caller@@:@@$context.identity.caller@@,@@cognito-authentication-provider@@: @@$context.identity.cognitoAuthenticationProvider@@,@@cognito-authentication-type@@:@@$context.identity.cognitoAuthenticationType@@,@@cognito-identity-id@@: @@$context.identity.cognitoIdentityId@@,@@cognito-identity-pool-id@@:@@$context.identity.cognitoIdentityPoolId@@,@@http-method@@:@@$context.httpMethod@@,@@stage@@: @@$context.stage@@,@@source-ip@@:@@$context.identity.sourceIp@@,@@user@@:@@$context.identity.user@@,@@user-agent@@:@@$context.identity.userAgent@@,@@user-arn@@: @@$context.identity.userArn@@,@@request-id@@:@@$context.requestId@@,@@resource-id@@:@@$context.resourceId@@,@@resource-path@@:@@$context.resourcePath@@}")
#set($inputString = '')
#set($allParams = $input.params())
{
"stateMachineArn": "my-state-machine-arn",
#set($inputString = "$inputString,@@body@@: $input.body")
#if ($includeHeaders)
#set($inputString = "$inputString, @@header@@:{")
#foreach($paramName in $allParams.header.keySet())
#set($inputString = "$inputString @@$paramName@@: @@$util.escapeJavaScript($allParams.header.get($paramName))@@")
#if($foreach.hasNext)
#set($inputString = "$inputString,")
#end
#end
#set($inputString = "$inputString }")
#end
#if ($includeQueryString)
#set($inputString = "$inputString, @@querystring@@:{")
#foreach($paramName in $allParams.querystring.keySet())
#set($inputString = "$inputString @@$paramName@@: @@$util.escapeJavaScript($allParams.querystring.get($paramName))@@")
#if($foreach.hasNext)
#set($inputString = "$inputString,")
#end
#end
#set($inputString = "$inputString }")
#end
#if ($includePath)
#set($inputString = "$inputString, @@path@@:{")
#foreach($paramName in $allParams.path.keySet())
#set($inputString = "$inputString @@$paramName@@: @@$util.escapeJavaScript($allParams.path.get($paramName))@@")
#if($foreach.hasNext)
#set($inputString = "$inputString,")
#end
#end
#set($inputString = "$inputString }")
#end
## Check if the request context should be included as part of the execution input
#if($requestContext && !$requestContext.empty)
#set($inputString = "$inputString,")
#set($inputString = "$inputString @@context@@: $requestContext")
#end
#set($inputString = "$inputString}")
#set($inputString = $inputString.replaceAll("@@",'"'))
#set($len = $inputString.length() - 1)
"input": "{$util.escapeJavaScript($inputString.substring(1,$len))}"
}
Hi Abhay,
How about serializing entire request context, parameters, stage variables, and body into a single JSON object? This involves dynamically constructing a JSON string that incorporates all parts of the request. Below is an example which captures everything: capture everything: the context, stage variables, parameters (query string parameters, path parameters, and headers), and the request body:
#set($allParams = $input.params())
#set($contextMap = {
"account-id" : "$context.identity.accountId",
"api-id" : "$context.apiId",
"api-key" : "$context.identity.apiKey",
"authorizer-principal-id" : "$context.authorizer.principalId",
"caller" : "$context.identity.caller",
"cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider",
"cognito-authentication-type" : "$context.identity.cognitoAuthenticationType",
"cognito-identity-id" : "$context.identity.cognitoIdentityId",
"cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId",
"http-method" : "$context.httpMethod",
"stage" : "$context.stage",
"source-ip" : "$context.identity.sourceIp",
"user" : "$context.identity.user",
"user-agent" : "$context.identity.userAgent",
"user-arn" : "$context.identity.userArn",
"request-id" : "$context.requestId",
"resource-id" : "$context.resourceId",
"resource-path" : "$context.resourcePath"
})
#set($stageVariables = $context.stageVariables)
#set($body = $input.json('$'))
#set($inputObject = {
"context" : $util.toJson($contextMap),
"stage-variables" : $util.toJson($stageVariables),
"params" : $util.toJson($allParams),
"body" : $body
})
#set($inputString = $util.toJson($inputObject))
{
"input": "$util.escapeJavaScript($inputString)",
"stateMachineArn": "my-state-machine-arn"
}
This script does the following: Constructs a map for the context and stage variables. Uses $input.params() to gather all parameters (query string, path, headers) automatically. Serializes the context, stage variables, parameters, and body into a JSON object. Converts the entire JSON object into a string to be used as the input for the Step Function. Make sure to replace "my-state-machine-arn" with the actual ARN of your state machine.
Hope this helps!
Contenus pertinents
- Réponse acceptéedemandé il y a 8 mois
- demandé il y a un an
- demandé il y a 7 jours
- demandé il y a 7 mois
- AWS OFFICIELA mis à jour il y a 3 ans
- AWS OFFICIELA mis à jour il y a un an
- AWS OFFICIELA mis à jour il y a un an
- AWS OFFICIELA mis à jour il y a un an
This does not work, Because "$util.toJson()" is not a function in the API Gateway Docs. Thanks for the reply.