首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >检测OpenWhisk Web Actions

检测OpenWhisk Web Actions

作者头像
用户1207313
发布2018-01-18 10:11:52
8950
发布2018-01-18 10:11:52

我已经写了一篇文章,它是关于OpenWhisk Web actions,以及他们是如何让你通过向客户端发送一个状态码和HTTP头后,在main()方法中得到一个带有键状态,标题和正文的字典返回:

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

如果这个测试action位于默认命名空间,那么我们用命令wsk action update test创建它

用命令test.swift -a web-export true去开启Web Action支持并通过以下的curl访问它:

curl https://openwhisk.ng.bluemix.net/api/v1/experimental/web/19FT_dev/default/test.http

<root> Hello world </ root>

但是,当你调用一个认证的POST API(例如通过curl或者wsk action 调用),你会看到:

$ 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"
  }
}

这是能被预见的,因为认证的POST API调用只是执行操作并发送它返回的内容。

Web Action中的其他参数

当您的操作被称为Web操作时,则会有额外参数不会以其他方式显示。我们可以简单地看下其中一个。例如,我选择查找__ow_meta_verb。

这样做的简单方法:

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",
        ],
    ]
}

请注意,我们得到一个字典返回,正如调用认证的POST API所期望的。通过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"
}

(我们通过这种方式只能获得JSON)

当然,调用web action并没有改变,我们仍然能得到XML。

我们可以在任何情况下运用合适的方法,并产生正确的响应。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Web Action中的其他参数
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档