0

Looking to understand how get to API Gatway Post vars (event['body']['param']) passed to my C# Lambda Funcion?

I have:

public string FunctionHandler(Stream request, ILambdaContext context)
{ StreamReader reader = new StreamReader(request);
 return reader.ReadToEnd();
}

but, using postman (Post Key Value: "Hello:world") it returns:

'\"body-json\" : \"LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLTcwMTQxNDY5ODUyNjQwNzI2NDM3ODU2OQ0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJUaGlzS2V5CiINCg0KV2hhdA0KLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLTcwMTQxNDY5ODUyNjQwNzI2NDM3ODU2OS0tDQo='

(No idea what that is supposed to represent)

If I do:

public string FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
{   return "request body is:" + request?.Body; }

I Get "request body is:"

It's an empty body?

Where is the post values?

This is using form-data and the body mapping is:

"##  See http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
##  This template will pass through all parameters including path, querystring, header, stage variables, and context through to the integration endpoint via the body/payload
#set($allParams = $input.params())
{
"body-json" : $input.json('$'),
"params" : {
#foreach($type in $allParams.keySet())
    #set($params = $allParams.get($type))
"$type" : {
    #foreach($paramName in $params.keySet())
    "$paramName" : "$util.escapeJavaScript($params.get($paramName))"
        #if($foreach.hasNext),#end
    #end
}
    #if($foreach.hasNext),#end
#end
},
"stage-variables" : {
#foreach($key in $stageVariables.keySet())
"$key" : "$util.escapeJavaScript($stageVariables.get($key))"
    #if($foreach.hasNext),#end
#end
},
"context" : {
    "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"
    }
}

2 Answers2

1

Your returned string is a base64 encoded rendition of:

----------------------------669263384077094357339492
Content-Disposition: form-data; name="Helloworld"

What
----------------------------669263384077094357339492--
Shirker
  • 11
  • 1
0
{
    "body" : "$util.escapeJavaScript($input.json('$'))"
}
Dmitry Grinko
  • 13,806
  • 14
  • 62
  • 86
  • No, still gives me the Giberish for the body: \"body\" : \"\\\"LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLTY2OTI2MzM4NDA3NzA5NDM1NzMzOTQ5Mg0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJIZWxsb3dvcmxkIg0KDQpXaGF0DQotLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tNjY5MjYzMzg0MDc3MDk0MzU3MzM5NDkyLS0NCg== –  Oct 11 '17 at 19:10
  • Did you use content type - application/json ? – Dmitry Grinko Oct 11 '17 at 19:39
  • No, I guess i wasn't that clear but, I'm using mulitpart/form-data as the content type because I need to send binary information (An image). I've set up the binary support to use multipart/form-data as well based on this lesson: [link](https://aws.amazon.com/blogs/compute/binary-support-for-api-integrations-with-amazon-api-gateway/) –  Oct 12 '17 at 18:42
  • API Gateway now supports binary payloads. Simply define "multipart/form-data" as a binary media type for your API and proxy the payload directly to a Lambda function. From there you can parse the body to get your file content. There should be libraries available to help parse the multipart body ('parse-multipart' in NodeJS for example) https://stackoverflow.com/a/41770688/6108211 – Dmitry Grinko Oct 12 '17 at 19:31
  • yes, that is exactly what I believe I have done. But, the body is the examples I give. I can't seem to understand the encoding. Every way I try to read it, it's Gibberish. –  Oct 12 '17 at 19:58