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

Lucas Jellema
0 0
Read Time:2 Minute, 51 Second

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"

About Post Author

Lucas Jellema

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director and Oracle Developer Champion. Solution architect and developer on diverse areas including SQL, JavaScript, Kubernetes & Docker, Machine Learning, Java, SOA and microservices, events in various shapes and forms and many other things. Author of the Oracle Press book Oracle SOA Suite 12c Handbook. Frequent presenter on user groups and community events and conferences such as JavaOne, Oracle Code, CodeOne, NLJUG JFall and Oracle OpenWorld.
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Next Post

Quickest route to Your First Handson Experience with GraalVM - Katacoda Scenario with live environment and step by step tutorial

TLDR: This Katacoda Scenario has a live runtime environment with GraalVM 20.1 prepared for you including an easy to click-through step by step scenario that does a live demonstration of most aspects of GraalVM (modern Java runtime, Ahead of Time compilation to native executable, polyglot runtime platform and the platform […]
%d bloggers like this: