title: Access HTTP request (headers, query string, cookies, body) object in lambda with http endpoint
body: <p>I am trying to see how to access the request header and body values from with in the lambda code. If the request body is in JSON format, it automatically seems to be parsed and made available in the event object.</p>
  
  <blockquote>
    <p>How can I access the complete query string, request body, request headers (cookies) for any type of incoming "Content-Type" request inside Lambda ?</p>
  </blockquote>
  
  <hr>
  
  <p><em>The edits below are information I have gathered to help solve the question that may or may not be relevant. Please ignore them if you wish to.</em></p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>I went through the existing questions on SE <a href="https://stackoverflow.com/questions/31372167/how-to-access-http-headers-for-request-to-aws-api-gateway-using-lambda?rq=1">here</a> and <a href="https://stackoverflow.com/questions/31329958/how-to-pass-a-querystring-or-route-parameter-to-aws-lambda-from-amazon-api-gatew?rq=1">here</a>. 
  As per this <a href="https://blog.hiramsoftware.com/blog/day-two-aws-api-gateway/index.html" rel="nofollow noreferrer">thread</a>, using <code>$input.json('$')</code> should do the trick. I guess the answers from these links above are already out-dated as API gateway by default seems to recognize JSON in the request and if so makes it available in the <code>event</code> object without any mapping templates being configured. </p>
  
  <p>Setting the mapping as suggested does not work for me. It does not contain the request header information.</p>
  
  <p>Here are screen shots on how it is configured.</p>
  
  <p><a href="https://i.stack.imgur.com/3ARB5.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3ARB5.gif" alt="enter image description here"></a></p>
  
  <hr>
  
  <p><a href="https://i.stack.imgur.com/2eVet.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2eVet.gif" alt="enter image description here"></a></p>
  
  <p>The "headers" key returns a blank value. Using <code>$input.params('$')</code> or <code>"$input.params('$')"</code> errors out.</p>
  
  <hr>
  
  <p>EDIT 2</p>
  
  <p>Tried defining the headers in Method Request. Still not getting the User-Agent value inside lambda.</p>
  
  <p><a href="https://i.stack.imgur.com/ZZulB.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZZulB.gif" alt="enter image description here"></a></p>
  
  <hr>
  
  <p>EDIT 3</p>
  
  <p>I used the following template mapping at the API Gateway</p>
  
  <pre><code>{
      "request": $input.json('$'),
      "headers": "$input.params()"
  }
  </code></pre>
  
  <p>and the below code in lambda</p>
  
  <pre><code>context.succeed("event.key32:"+JSON.stringify(event, null, 2) );
  </code></pre>
  
  <p>And the response generated by the API gateway shows this
  <a href="https://i.stack.imgur.com/kPRGm.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kPRGm.gif" alt="enter image description here"></a></p>
  
  <p>Looking at the "headers" value in the response, it looks like the AWS-SDK/API gateway/cloudfront strips off all headers received from the HTTP client ? Here is the full text from the JSON returned by the $input.params().header</p>
  
  <pre><code>header={CloudFront-Forwarded-Proto=https, CloudFront-Is-Desktop-Viewer=true, CloudFront-Is-Mobile-Viewer=false, CloudFront-Is-SmartTV-Viewer=false, CloudFront-Is-Tablet-Viewer=false, Content-Type=application/json, Via=1.1 5d53b9570d94ce920abbd471.cloudfront.net (CloudFront), 1.1 95eea7baa7ec95c9a41eca9e3ab7.cloudfront.net (CloudFront), X-Amz-Cf-Id=GBqmObLRy6Iem9bJbVPrrW1K3YoWRDyAaMpv-UkshfCsHAA==, X-Forwarded-For=172.35.96.199, 51.139.183.101, X-Forwarded-Port=443, X-Forwarded-Proto=https}}
  </code></pre>
  
  <p>It doesn't have the User-Agent string in the header, although as the screenshot shows above, it was sent by the REST client.
  Interestingly, the entire query string is made available. Not sure if this is an intended way to access it.</p>