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

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년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠