I've set up an API Gateway GET method integrated with a bare-bones AWS Lambda function. I've enabled the Request Body Passthrough on the Integration Request to the pre-set Method request passthrough template.
I would like to do different things based on the resource-path of the request. For example, if the path is /foo, I would interpret the foo request, or /bar would interpret as the bar request, with the same Lambda function. So I need to switch based on the resource-path within the Lambda function itself.
I'm getting stuck accessing the mapped payload template. All the data should be there according to the AWS help. But in java, I can't figure out how to convert the input from API gateway into parsable json using Jackson, org.json, or json-path.
Here is my Lambda code. Any help on how to get "resource-path", or any of the method request passthrough, from the API Gateway GET would be appreciated.
import org.json.JSONObject;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class LambdaFunctionHandler implements RequestHandler<Object, Object> {
@Override
public Object handleRequest(Object input, Context context) {
JSONObject inputJson = new JSONObject(input.toString());
JSONObject contextJson = inputJson.getJSONObject("context");
String resourcePath = contextJson.getString("resource-path");
return resourcePath;
}
}
And here is what I believe is being sent into the function as input:
{
"body-json" : {},
"params" : {
"path" : {},
"querystring" : {},
"header" : {}
},
"stage-variables" : {},
"context" : {
"account-id" : "xxxxxxxxx",
"api-id" : "xxxxxxxxx",
"api-key" : "xxxxxxxxx",
"authorizer-principal-id" : "",
"caller" : "xxxxxxxxx",
"cognito-authentication-provider" : "",
"cognito-authentication-type" : "",
"cognito-identity-id" : "",
"cognito-identity-pool-id" : "",
"http-method" : "GET",
"stage" : "test-invoke-stage",
"source-ip" : "test-invoke-source-ip",
"user" : "xxxxxxxxxxxxxxxx",
"user-agent" : "Apache-HttpClient/4.5.x (Java/1.8.0_112)",
"user-arn" : "arn:aws:iam::230537478972:root",
"request-id" : "test-invoke-request",
"resource-id" : "75bakm",
"resource-path" : "/text"
}
}
But I'm getting:
{
"errorMessage": "Expected a ':' after a key at 11 [character 12 line 1]",
"errorType": "org.json.JSONException",
"stackTrace": [
"org.json.JSONTokener.syntaxError(JSONTokener.java:433)",
"org.json.JSONObject.<init>(JSONObject.java:216)",
"org.json.JSONObject.<init>(JSONObject.java:323)",
"xxx.LambdaFunctionHandler.handleRequest(LambdaFunctionHandler.java:12)"
]
}