Location header lost from Python response

0

I'm returning a "Location" header from my POST handler. Here is how the response is setup:

body = { 'data': 1 } # Dummy for this example  
location = 'https://my.web.site/url' # dummy value for this example  
headers = { 'location': location }  
result = { 'statusCode': 200, 'body': json.dumps(body),  'headers'=headers}  

This works when I use the lambda locally (sam local start-api), but when I deploy on AWS Lambda and invoke the functions through its HTTPS URL, the response does not contain the Location header.

(Also, I now fail to find the docs on the Python lambda handler return value dictionary. I don't remember where I found the statusCode, body and headers docs)

asked 4 years ago774 views
4 Answers
0

Assuming you're using a lambda proxy integration from api gateway, the output dict documentation is here: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format

You have an = in you result dict, which should be a syntax error.

As an aside, the location header usually isn't much use with a 2xx response. Usually needs a 3xx.

Ellison
answered 4 years ago
0

The = in the dict is a red herring caused by careless simplification of the code for illustration.

201 CREATED response usually contain a location header. (See for example: https://restfulapi.net/http-status-201-created/)

Again, the fact the a given HTTP code usually has a location header or not is mostly irrelevant. The problem is that the code works with lambda run locally but not on AWS.

answered 4 years ago
0

Well, at this point I'd say there's no easy answer. There could be some difference in how sam is set up as opposed to api gateway and lambda. There could be some other problem. If you can, please post the (suitably redacted) lambda and the api gateway config?

Ellison
answered 4 years ago
0

I created a simplified version and I found the problem:

  • When run locally, the header is Location (Uppercase L)
  • When run when deployed, the header is location (lowercase L)

The API gateway changes the headers case. The test I ran was too strict and expected the case not to change.

answered 4 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions