2

I am using AWS Lambda and API Gateway and facing an issue with my API's response code,
In case of an exception I am setting responseCode as 400 in my response,
But the API status is 200.

I found that my solution is related to AWS API Gateway's Integration Response,
I created all the possible Http Status codes that my application needs,
And I set the Lambda Error Regex in Integration Response,
But I still get API status as 200 despite me sending "responseCode" : "400", in my APIs response(response type is JsonObject in Java).

I have the following code AWS Lambda code,
I am passing ErrorCode as query parameter to my Get method,
And returning the same Error code in response eg. "responseCode" : "400".

My expected output is "responseCode" : "400" but, the API's status should also become 400 instead of 200.

public class MainHandler implements RequestHandler<JSONObject, JSONObject> {

    public JSONObject handleRequest(JSONObject request, Context context) {

        try {
            JSONObject requestJson = (JSONObject) new JSONParser().parse(request.toString());
            System.out.println("RequestJson : " + requestJson);

            JSONObject params = (JSONObject) requestJson.get("params");
            System.out.println("Params : " + params);

            JSONObject queryString = (JSONObject) params.get("querystring");
            System.out.println("QueryString : " + queryString);

            String error = (String) queryString.get("errorCode");
            System.out.println("ErrorCode : " + error);

            JSONObject resp = new JSONObject();
            resp.put("responseCode", error);

            return resp;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

AWS API Gateway Method Response - enter image description here

AWS API Gateway Integration Response - enter image description here

Postman response -
The API status shows 200 Ok, whereas I want it as 400. enter image description here

I am referring the following links -
1. https://forums.aws.amazon.com/thread.jspa?threadID=192918
2. Is there a way to change the http status codes returned by Amazon API Gateway?

Dev1ce
  • 5,390
  • 17
  • 90
  • 150

1 Answers1

3

Your Lambda Error Regex is not a regular expression. Changing it to .*"responseCode":\s"400".* should do it.

But for that use case it would be better to activate Lambda proxy integration. This provides a more flexible way to set the response code directly from the lambda function.

teyzer
  • 1,440
  • 1
  • 10
  • 14
  • Hi, it didn't work :/, I've added the screenshot of the Integration Response – Dev1ce Jan 02 '18 at 11:14
  • 1
    Maybe the problem is that your function is not failing. Try throwing an error like `throw new Exception("code 400")` and set the lambda error regex to `.*code\s400.*` (docs https://docs.aws.amazon.com/lambda/latest/dg/java-exceptions.html and https://docs.aws.amazon.com/apigateway/latest/developerguide/handle-errors-in-lambda-integration.html) – teyzer Jan 02 '18 at 21:47