DZone
Cloud Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Cloud Zone > Detecting OpenWhisk Web Actions

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.

Rob Allen user avatar by
Rob Allen
·
Mar. 28, 17 · Cloud Zone · Tutorial
Like (1)
Save
Tweet
2.75K Views

Join the DZone community and get the full member experience.

Join For Free

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.

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Writing Beautiful, Optimized, and Better .NET Code With NDepend Static Analysis
  • Exhaustive JUNIT5 Testing with Combinations, Permutations, and Products
  • SQL Database Schema: Beginner’s Guide (With Examples)
  • Version Number Anti-Patterns

Comments

Cloud Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo