OpenWhisk Actions on Knative — Actions with Access to HTTP Body Parameters

Priti Desai
Apache OpenWhisk
Published in
2 min readMay 14, 2019

This is a follow up blog post of How to run OpenWhisk actions on Knative?

OpenWhisk web actions when invoked receives HTTP request details as parameters to the action input argument. For example, the following web action returns response with __ow_method, __ow_headers, __ow_path, etc:

params.js

Invoking params.js with Empty JSON Object

curl -H "Host: <HOST>" -X POST http://<IP_ADDRESS>:<PORT> -H 'Content-Type: application/json' -d '{"value": {}}'
{
"response":{
"__ow_body":"e30=",
"__ow_user":"",
"__ow_method":"POST",
"__ow_headers":{
"host":"localhost:8080",
"user-agent":"curl/7.54.0",
"accept":"*/*",
"content-type":"application/json",
"content-length":"377"
},
"__ow_path":""
}
}

Note: e30= can be decoded to {}

Invoking params.js with Non Empty JSON Object

curl -H "Host: <HOST>" -X POST http://<IP_ADDRESS>:<PORT> -H 'Content-Type: application/json' -d '{"value": {"name":"Joe", "place": "OK"}}'
{
"response":{
"name":"Joe",
"place":"OK",
"__ow_body":"eyJuYW1lIjoiSm9lIiwicGxhY2UiOiJPSyJ9",
"__ow_user":"",
"__ow_method":"POST",
"__ow_headers":{
"host":"localhost:8080",
"user-agent":"curl/7.54.0",
"accept":"*/*",
"content-type":"application/json",
"content-length":"412"
},
"__ow_path":""
}
}

Note: eyJuYW1lIjoiSm9lIiwicGxhY2UiOiJPSyJ9 can be decoded to {“name”:”Joe”, “place”: “OK”}

Now, a web action may elect to interpret and process an incoming HTTP body directly without having JSON object in the action input i.e. params.name vs params.__ow_body.

__ow_body is a string containing the request body entity, as a base64 encoded string when content is binary or JSON object and plain string otherwise.

Let’s learn how to deploy such action on Knative:

Pre-requisites:

  • Secret exists on Knative, check with:
$ kubectl get secret dockerhub-user-pass
  • Service Account exists on Knative, check with:
$ kubectl get serviceaccount openwhisk-runtime-builder
  • Build Template exists on Knative, check with:
$ kubectl get buildtemplate openwhisk-nodejs-runtime

If any of the secret/service account/build template does not exist on your Knative installation, please go back to the How to run OpenWhisk actions on Knative? blog post and run Step 1 through Step 3 depending on what’s missing.

Deploy NodeJS Runtime with Params Web Action

Configure the build file to point to your Docker Hub repo by replacing DOCKER_USERNAME with your username.

Note: OpenWhisk NodeJS runtime reads OW_ACTION_RAW argument which is set to true and marks the action as OpenWhisk raw HTTP web action.

build.yaml

Deploy NodeJS runtime with action code:

$ kubectl apply -f build.yaml
build.build.knative.dev/nodejs-10-web-action-raw created

Verify the build pod exists:

$ kubectl get build.build.knative.dev/nodejs-10-web-action-raw
NAME SUCCEEDED REASON STARTTIME COMPLETIONTIME
nodejs-10-web-action-raw True 1m

Serve NodeJS Runtime as a Knative Service

Configure the service template to point to the Docker Hub repo where the OpenWhisk runtime (built in previous step) will be pulled from. Replace ${DOCKER_USERNAME} and create service.yaml:

service.yaml

Run Raw Web Action

Depending on your Knative installation, determine how to access your knative gateway and run sequence of curl:

curl -H "Host: nodejs-web-action-raw.default.example.com" -X POST http://<IP_ADDRESS>:<PORT> -H 'Content-Type: application/json' -d '{"value": {"name":"Joe", "place": "OK"}}'
{
"response":{
"name":"Joe",
"place":"OK",
"__ow_body":"eyJuYW1lIjoiSm9lIiwicGxhY2UiOiJPSyJ9",
"__ow_user":"",
"__ow_method":"POST",
"__ow_headers":{
"host":"localhost:8080",
"user-agent":"curl/7.54.0",
"accept":"*/*",
"content-type":"application/json",
"content-length":"412"
},
"__ow_path":""
}
}

We can verify the content of __ow_body by decoding the body content:

decode.js
curl -H "Host: nodejs-decode.default.example.com" -X POST http://<IP_ADDRESS>:<PORT> -H 'Content-Type: application/json' -d '{"value": {"name":"Joe", "place": "OK"}}'
{"name":"Joe","place":"OK"}

Enjoy! 👍

--

--

Priti Desai
Apache OpenWhisk

Developer Lead @IBM. Tekton maintainer. Co-founder of License Scanner @Cyclonedx