Oracle Cloud Infrastructure Functions and Project Fn - Retrieving Headers, Query Parameters and other HTTP Request elements image 81

Oracle Cloud Infrastructure Functions and Project Fn – Retrieving Headers, Query Parameters and other HTTP Request elements

Functions on Oracle Cloud Infrastructure are implemented using Project Fn. A function runs in Docker container. This container has a runtime (for example for Java, Go, Python, Node) and a minimal handler to handle an HTTP request and turn it into a call to the function. When writing the code to implement the function, we do not need to be aware of this layout. The FDK wrapper hands our Node function or Java method one or more parameters from which we can take what we need in terms of input.

image

For example with a Node implementation, our function gets parameter input which contains the input payload, taken from the HTTP request’s body. We can also accept a second argument – by convention called ctx. This argument gives us a handle to many details from the original triggering HTTP request:

  • headers
  • full URL path – including all path-segments and query parameters
  • Fn runtime details (CPU, memory, version, Fn call id, ..)
  • Function meta data (OCID for application and function)
  • HTTP Method (GET, POST, PUT, …)
  • OPC Request Id
  • IP of the requesting system

The ctx parameter looks like this for an HTTP request:

image

Standard headers such as Host, User-Agent, Content-Type can be accessed under _headers in the context parameters. Custom HTTP Headers are included with a special prefix: Fn-Http-H-

image

The original URL can be found in _headers using the key “Fn-Http-Request-Url”. The HTTP Method is available under “Fn-Http-Method”.

image

With the data available in the context, we can interpret many details from the original HTTP request and respond accordingly.

Note: in these screenshots, some of the headers were added by the OCI API Gateway that sat between my HTTP client (Postman) and the Function running on OCI.