简体   繁体   中英

How to pass POST variables to an AWS API Gateway running a lambda function?

I'm using the following code to attempt to hit my aws api gateway running a simple test lambda function.

public class AWSTest {

public static void main(String[] args) {
    HttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost("AWS API GATEWAY LINK HERE");

    try {
        HttpResponse response = client.execute(post);

        String json = "{ \"key1\":\"username\", \"key2\":\"password\", \"key3\":\"value3\" }";

        List<NameValuePair> argys = new ArrayList<>();
        argys.add(new BasicNameValuePair("key1", "key1"));

        post.setEntity(new StringEntity(json));

        System.out.println(EntityUtils.toString(response.getEntity()));
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

My Lambda functions is as follows:

exports.handler = (event, context, callback) => {
// TODO implement
callback(null, 'Hello from ' + event.key1);
};

The result I receive is:

"Hello from undefined"

The API Resource that hits the lambda function is using the POST method. There's no API Key or any sort of authentication as this is just a test.

What is the best way to get the information accross the API Gateway to the Lambda function? I also intend to use this Lambda function in the future as a login authentication so also let me know if what I'm doing is not good practice and there's a better way. Thanks!

The best way to do what you want is to set up your API endpoint as a proxy for the lambda. In order to do so it requires just two steps.

Fisrt, when creating the POST method for your resource in the API Gateway console, select Use Lambda Proxy integration in the configuration step where you select the lambda that your method will call. This will wrap your request before send it to the lambda in a very handy format that will allow your lambda access not only the body fo your POST request but also all info contained in the url, headers, etc Concretely the format will be:

{
    "resource": "Resource path",
    "path": "Path parameter",
    "httpMethod": "Incoming request's method name"
    "headers": {Incoming request headers}
    "queryStringParameters": {query string parameters }
    "pathParameters":  {path parameters}
    "stageVariables": {Applicable stage variables}
    "requestContext": {Request context, including authorizer-returned key-value pairs}
    "body": "A JSON string of the request payload."
    "isBase64Encoded": "A boolean flag to indicate if the applicable request payload is Base64-encode"
}

The json object of your example will be contained in the body of the wrapper json above.

Second, you just need to modify your lambda to read that object. Simply access the body by doing

event.body

You can see a whole example in the AWS docs here

Regarding your second question about authentication, you shouldn't have any problem to implement it via API + Lambda. Everything will depend on how you implement authentication. Spring OAtuth could be a good option. You could have a lambda to generate an access token and access that lambda via the API, but that is just an option among a lot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM