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
gefragt vor 5 Jahren1102 Aufrufe
1 Antwort
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
beantwortet vor 5 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen