We now find ourselves in the position where we have working code and scripts that can redeploy that code: what is commonly known as "Green".
But the objective of our code at the moment is to output "hello, world" and it doesn't do that. We need to return a body output somehow.
The output we are providing is in the IgnorantResponse class, but at the moment that's just an empty class. We need to update it to return a body.
As far as I can tell, all of this works by Amazon magic using Java reflection on beans.
Lambda Proxy Communication
You can see what the lambda proxy input looks like by going back and studying the output from testing the API in the UI. About halfway down there is a line that starts "Endpoint request body after transformations:". Looking along that, you can see a big JSON structure which has fields called "resource", "path" and "httpMethod" among others. If you are familiar with how HTTP works, all of this should seem very reasonable, even if some of it is AWS specific and generally there is too much information so it ends up being truncated.
Fortunately, the input and output formats are described in the AWS documentation.
So, fairly obviously, we want to set the body field of the response.
Fortunately, the input and output formats are described in the AWS documentation.
So, fairly obviously, we want to set the body field of the response.
Creating a Response
Because AWS uses Java reflection - and assumes our response object is a POJO or bean, all we should need to do is to create the getBody() method on our IgnorantResponse and have it return the relevant message.
public class IgnorantResponse {
public String getBody() {
return "hello, world";
}
}
We can then repackage and retry our curl command:
scripts/package.sh
curl https://tovogqsfoj.execute-api.us-east-1.amazonaws.com/ignorance/hello
We can also have the handler specifically set the status code. This, for example, will return a 204 with no data:public class IgnorantResponse {
public int getStatusCode() {
return 204;
}
public String getBody() {
return "";
}
}
In order to see the status code, you will need to use curl -v.Conclusion
Working with the Response object is fairly simple: we just create the appropriate POJO getters for the fields that we want to populate in the lambda response.
You can find all the code in the repository tagged API_GATEWAY_HELLO_WORLD.
Next: We Need to Handle Input
Next: We Need to Handle Input
No comments:
Post a Comment