Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting http response header from AWS lambda

My API Gateway/Lambda setup returns an HTTP response header: Lambda uses callback function to return the value as part of a JSON and the Integration Response maps it into a HTTP header (using integration.response.body)

With this solution, the values are sent back both in the body and the header.

How can I map headers from the Lambda response without duplicating the values in the response body?

like image 511
Saar Avatar asked Apr 03 '17 17:04

Saar


People also ask

How do you pass headers in Lambda?

To pass custom headers from an API Gateway API to a Lambda function, use a body mapping template. The API sends the updated API request to a Lambda function to process the headers. Then, the Lambda function returns one or more header values from the original API request.

How do I add HTTP response header?

Select the web site where you want to add the custom HTTP response header. In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add. In the Name box, type the custom HTTP header name.


2 Answers

If you have Lambda proxy integration enabled, you can set the response headers as part of Lambda output and API Gateway will return them as part of the HTTP response to the client.

Node.js example:

callback(null, {     "isBase64Encoded": false, // Set to `true` for binary support.     "statusCode": 200,     "headers": {         "header1Name": "header1Value",         "header2Name": "header2Value",     },     "body": "...", }); 

where headers can be null or unspecified if no extra response headers are to be returned.

See Output Format of a Lambda Function for Proxy Integration.

like image 69
Khalid T. Avatar answered Sep 19 '22 12:09

Khalid T.


and, if you DON'T have Lamba proxy integration enabled, you can add (and map) the response headers in the amazon API gateway console:

go to resources -> method execution -> method response -> add 'Access-Control-Allow-Origin' (or whatever) header for http status 200. Then go back to method execution -> integration response -> http status 200 -> set header mapping for 'Access-Control-Allow-Origin' to '*' (or whatever).

Solved this error...: "No 'Access-Control-Allow-Origin' header is present on the requested resource"

like image 22
B0773N Avatar answered Sep 22 '22 12:09

B0773N