Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Detecting OpenWhisk Web Actions

DZone's Guide to

Detecting OpenWhisk Web Actions

OpenWhisk actions are great, but they're only half the equation. Detecting them with a POST API or detecting additional arguments requires extra legwork.

· Cloud Zone
Free Resource

Make it happen: rapid app development on Kubernetes as a managed service.

I've already written about OpenWhisk web actions and how they allow you to send a status code and HTTP headers to the client by returning a dictionary with the keys status, headers, and body from your main() method:

func main(args: [String:Any]) -> [String:Any] {
    return [
        "body": "<root>Hello world</root>",
        "code": 200,
        "headers": [
            "Content-Type": "text/xml",
        ],
    ]
}


If this test action is in the default namespace, then we create it with wsk action update test
test.swift -a web-export true to enable web action support and access it via curl:

curl https://openwhisk.ng.bluemix.net/api/v1/experimental/web/19FT_dev/default/test.http
 
<root>Hello world</root>


However, when you invoke this via the authenticated POST API (eg. via curl or wsk action invoke), you get this:

$ AUTH=$(wsk property get --auth | awk '{printf("%s", $3)}' | openssl base64 | tr -d "\n")
$ curl -X POST -H "Authorization: Basic $AUTH" \
"https://openwhisk.ng.bluemix.net/api/v1/namespaces/19FT_dev/actions/test?blocking=true&result=true"
 
{
  "body": "<root>Hello world</root>",
  "code": 200,
  "headers": {
    "Content-Type": "text/xml"
  }
}


This could have been predicted, as the authenticated POST API call just executes the action and sends back what it returned.

Additional Arguments in a Web Action

When your action is called as a web action, then there are additional arguments that don't appear otherwise. We can simply look for one of these. Specifically, I chose to look for __ow_meta_verb.

The simple way of doing this:

func main(args: [String:Any]) -> [String:Any] {
 
    if args["__ow_meta_verb"] == nil {
        return ["root": "Hello world"]
    }
    return [
        "body": "<root>Hello world</root>",
        "code": 200,
        "headers": [
            "Content-Type": "text/xml",
        ],
    ]
}


Note that we return a dictionary, as an authenticated POST API call expects this. Calling internally via curl:

$ curl -X POST -H "Authorization: Basic $AUTH" \
"https://openwhisk.ng.bluemix.net/api/v1/namespaces/19FT_dev/actions/test?blocking=true&result=true"
 
{
  "root": "Hello world"
}


(We can only get JSON back this way)

And, of course, calling the web action hasn't changed, and we still get our XML.

We can call our function with whatever mechanism is appropriate and generate the right response.

Tutorial: WordPress as distributed microservices on Kubernetes.

Topics:
openwhisk ,cloud ,tutorial ,web actions ,serverless

Published at DZone with permission of Rob Allen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}