Reading Query Parameters in GET requests to Project Fn Functions (and Oracle Functions as a Service) image 9

Reading Query Parameters in GET requests to Project Fn Functions (and Oracle Functions as a Service)

TLDR: query parameters can be retrieved from the Fn-Http-Request-Url header in the ctx parameter passed by Fn to the Node function handling the request; using url.parse() on that header retrieves the url query parameters

 

imageProject Fn is an open source container native serverless framework. Project Fn provides a runtime framework that handles HTTP requests and delivers them to an instance of the function to handle the request. This instance is implemented through a Docker container instance. Project Fn is the foundation for Oracle’s FaaS offering – Oracle Functions on Oracle Cloud Infrastructure.image

 

Project Fn comes with out of the box support for various runtimes. In addition, custom Docker containers can be used as function implementation. In the case of Node as runtime, a new function is initialized with a wrapper application called func.js by default. The image shows the creation of a new function and the contents of the wrapper function func.js that is generated.

image

The input parameter that is defined will be provided by the Fn framework when a request is handled. This parameter contains the HTTP Body of the request, typically JSON content in a POST request.

In case your function needs to know the value of the headers in the request, you can add a second input parameter to your function; this parameter is commonly called ctx:

image

The ctx parameter is an object with details on the request, such as headers, original caller, configuration values defined with Fn, and the original request’s query parameters.

 Fn Server handling an HTTP request

 

A full list of the contents of the ctx object:

  • ctx.config : An Object containing function config variables (from the environment ) (read only)
  • ctx.headers : an object containing input headers for the event as lists of strings (read only)
  • ctx.deadline : a Date object indicating when the function call must be processed by
  • ctx.callID : The call ID of the current call
  • ctx.fnID : The Function ID of the current function
  • ctx.memory : Amount of ram in MB allocated to this function
  • ctx.contentType : The incoming request content type (if set, otherwise null)
  • ctx.setResponseHeader(key,values…) : Sets a response header to one or more values
  • ctx.addResponseHeader(key,values…) : Appends values to an existing response header
  • ctx.responseContentType set/read the response content type of the function (read/write)
  • ctx.httpGateway The HTTP Gateway context for this function (if set)

Finally, in order to get the values of the query parameters, in a Node application I make use of the following snippet of code where the Fn-Http-Request-Url header is located in the headers object in the ctx object and the contents of this header (itself an array) is parsed in the parse method in the standard node url library. The result is an object with a property for every query parameter.

image

 

const fdk = require('@fnproject/fdk');
const url = require('url');
fdk.handle(async function (input, ctx) {
  let requestURL = ctx.headers["Fn-Http-Request-Url"]
  let queryData
  if (requestURL) {
    queryData = url.parse(requestURL[0], true).query;
    console.log(`querydata = ${JSON.stringify(queryData)}`)
  }
  // make use of the query parameters that are found in the queryData object
  // for example work with the query parameter hashtag 
  // from a URL defined as http://host/path?hashtag=MY_TAG&otherparam=X
  const hashtag = queryData ?
    queryData.hashtag ? queryData.hashtag : "DEFAULT_TAG"
    : input.hashtag ? input.hashtag : "DEFAULT_TAG"