Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raw body payload in AWS API Gateway Body Mapping Template

For some reason I'm having a hard time getting the raw body from within the event. It's logging the $input.body as json for a application/json content-type. The docs say that that should contain the raw payload.

Here my Integration Request Body Mapping Template:

{
  "body" : $input.json('$'),
  "rawBody": $input.body,
  "headers": {
    #foreach($header in $input.params().header.keySet())
    "$header": "$util.escapeJavaScript($input.params().header.get($header))" #if($foreach.hasNext),#end

    #end
  },
  "method": "$context.httpMethod",
  "params": {
    #foreach($param in $input.params().path.keySet())
    "$param": "$util.escapeJavaScript($input.params().path.get($param))" #if($foreach.hasNext),#end

    #end
  },
  "query": {
    #foreach($queryParam in $input.params().querystring.keySet())
    "$queryParam": "$util.escapeJavaScript($input.params().querystring.get($queryParam))" #if($foreach.hasNext),#end

    #end
  }  
}

Here's the payload example:

{
  "event": {
    "body": {
      "hello": "meow"
    },
    "rawBody": {
      "hello": "meow"
    },
    "headers": {
      "Accept": "*/*",
      "Accept-Encoding": "gzip, deflate",
      "Accept-Language": "en-US",
      "Cache-Control": "no-cache",
      "CloudFront-Forwarded-Proto": "https",
      "CloudFront-Is-Desktop-Viewer": "true",
      "CloudFront-Is-Mobile-Viewer": "false",
      "CloudFront-Is-SmartTV-Viewer": "false",
      "CloudFront-Is-Tablet-Viewer": "false",
      "CloudFront-Viewer-Country": "US",
      "Content-Type": "application/json",
      "Host": "7nuy7lymef.execute-api.us-east-1.amazonaws.com",
      "Origin": "file://",
      "Postman-Token": "0ce7c6f4-3864-c9b4-f2db-739737b2ba49",
      "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Postman/4.2.2 Chrome/47.0.2526.73 Electron/0.36.2 Safari/537.36",
      "Via": "1.1 1eea0bca59557555878da4d9775c509f.cloudfront.net (CloudFront)",
      "X-Amz-Cf-Id": "SDjaGcuJ5eVkOMMCn6M3vGaVicA1fuA7h0bUYE4ARlKupO60eeYNFA==",
      "X-Forwarded-For": "206.71.230.14, 205.251.250.135",
      "X-Forwarded-Port": "443",
      "X-Forwarded-Proto": "https",
      "x_example_header": "my awesome header"
    },
    "method": "POST",
    "params": {},
    "query": {
      "example_param": "myawesomeparam"
    }
  },
  "context": {
    "callbackWaitsForEmptyEventLoop": false,
    "logGroupName": "/aws/lambda/reggi-log-post",
    "logStreamName": "2016/06/08/[$LATEST]aad04e0e46614c288ac8ca43d0a95076",
    "functionName": "reggi-log-post",
    "memoryLimitInMB": "128",
    "functionVersion": "$LATEST",
    "invokeid": "6e4e1e13-2dc1-11e6-a1f7-4dad3a8eb122",
    "awsRequestId": "6e4e1e13-2dc1-11e6-a1f7-4dad3a8eb122",
    "invokedFunctionArn": "arn:aws:lambda:us-east-1:562508364089:function:reggi-log-post"
  }
}
  • Is there any way to access the raw body from this request?
  • Is there any way to change the content-type to accept all types?
like image 746
ThomasReggi Avatar asked Jun 08 '16 21:06

ThomasReggi


People also ask

What is payload in API gateway?

In API Gateway, the API request and response have a text or binary payload. A text payload is a UTF-8 -encoded JSON string. A binary payload is anything other than a text payload. The binary payload can be, for example, a JPEG file, a GZip file, or an XML file.

What is VTL in API gateway?

API Gateway uses the following logic to select a mapping template, in Velocity Template Language (VTL) , to map the payload from a method request to the corresponding integration request or to map the payload from an integration response to the corresponding method response.

What are mapping templates in API gateway?

A mapping template is a script expressed in Velocity Template Language (VTL) and applied to the payload using JSONPath expressions . The payload can have a data model according to the JSON schema draft 4 .

Which feature of the API gateway can be used to format the data response?

API Gateway uses Velocity Template Language (VTL) engine to process body mapping templates for the integration request and integration response.


1 Answers

$input.body contains the raw payload. You need to put quotes around it like "rawBody": "$input.body". Otherwise the body will be interpreted as part of the json document.

like image 104
Abhigna Nagaraja Avatar answered Sep 18 '22 21:09

Abhigna Nagaraja