Ich bin mit AWS Lambda und API-Gateway und eine Frage, die mich mit meinem API-Response-Code,
Im Fall einer Ausnahme, die ich Response als 400 in meiner Antwort bin Einstellung
Aber der Status API ist 200.AWS API-Gateway Integration Antwort
ich fand, dass meine Lösung AWS API Gateway Integration Antwort verwandt ist,
ich alle möglichen HTTP-Statuscodes erstellt, die meine Anwendung benötigt,
Und ich die Lambda Fehler Regex in Integration WarteSet,
Aber ich bekomme immer noch API-Status als 200, obwohl ich gesendet habe "responseCode": "400", in meiner APIs-Antwort (Antworttyp ist JsonObject in Java).
Ich habe den folgenden Code AWS Lambda-Code,
I Error-Code als Abfrageparameter meine Get-Methode,
Und Rückkehr den gleichen Fehlercode in Reaktion zB bin vorbei. "Antwortcode": "400".
Meine erwartete Ausgabe ist "Response": "400", sondern der Status des API auch 400 statt 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-Methode Antwort werden sollte -
Integration der AWS-API-Gateway-Antwort -
Postman Antwort -
Die API-Status zeigt 200 Ok, während ich es als 400.
Ich möchte die folgenden Links beziehe mich -
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?
Hallo, es hat nicht funktioniert: /, Ich habe den Screenshot der Integration Response hinzugefügt –
Vielleicht ist das Problem, dass Ihre Funktion nicht fehlschlägt. Versuchen Sie, einen Fehler wie 'throw new Exception (" code 400 ") zu werfen und setzen Sie die Lambda-Fehler-Regex auf'. * Code \ s400. * '(Docs https://docs.aws.amazon.com/lambda/latest/ dg/java-exceptions.html und https://docs.aws.amazon.com/apigateway/latest/developerguide/handle-errors-in-lambda-integration.html) – teyzer