Sometimes (or in some cases, every time) when you invoke a custom API Service, you may need additional information about the context from which the HTTP request was sent/received, such as user or device information. To collect that information, we provide a class called InvocationContext.
This class has a number of convenient methods to determine various request parameters. Until recently, there were six such parameters:
Only the last one is related to the http request itself – getHttpHeaders.
Recently we added three more methods:
Let’s see what we can do with them now.
First, we need to download CodeRunner and create an API Service.
1) Download
2) Create service
For our example, let’s collect statistics about the geographical location of the visitors/users of our service. We’ll also add the ability to use an optional additional parameter to change the behavior of the service.
The code (please pay attention to the bold text):
The last step is to build the project and deploy the code, using ./bin/CodeRunner.sh.
That’s all there is to it. Thanks for reading, and happy coding!