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)

已提問 4 年前檢視次數 808 次
4 個答案
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
已回答 4 年前
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.

已回答 4 年前
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
已回答 4 年前
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.

已回答 4 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南