Lambda NodeJS 8.1 and HTTP Status <> 200

0

Trying to go serverless with apigateway but having an issue getting Lambda endpoints to return any HTTP Status Code other than 200. I can of course return JSON with status set to whatever I want, but I need to set the HTTP Status to match.

exports.handler = async (event, context, callback) => {

           return new Promise((resolve,reject) => {
                      findData().then((data) => {
                                // found data
                                resolve({
                                        status: 200,
                                        body: data
                                 });
                      },(err) => {
                               // didn't find anything
                               reject({
                                        status: 404,
                                        error: {
                                                message: 'not found'
                                       }
                                });
                       });
              });
};

I cannot figure out how to get lambda and apigateway to return HTTP Status Code 404 in the error case. I have also tried the documented

             {
                    statusCode: 404,
                    headers: {},
                    body: err
             }

But that does not work either. I have also change the error side of the promise to call resolve (instead of reject)...it of course works but the HTTP Status code is always 200 no matter what I try.

I am open to using Response Mappings, but that isn't so straight-forward and I cannot find any examples.....

Thanks for your time.

Edited by: rjsoph on Feb 10, 2019 1:59 PM

Edited by: rjsoph on Feb 10, 2019 2:01 PM

rjsoph
已提问 5 年前1080 查看次数
1 回答
1

Found a reference that worked......

You must not select "Lambda proxy integration" on apigateway for this to work.....

Node 8 you call resolve and reject.....then using the below reference you setup the mappings and templates....worked perfectly for me.

https://kennbrodhagen.net/2016/03/09/how-to-return-a-custom-error-object-and-status-code-from-api-gateway-with-lambda/

rjsoph
已回答 5 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则