Web Action返回带有空主体的HTTP 200响应 [英] Web Action returns HTTP 200 response with empty body

查看:138
本文介绍了Web Action返回带有空主体的HTTP 200响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经从CLI创建了OpenWhisk Web Action,通过公共Web Action URL调用该操作始终会返回一个空的响应正文.返回的HTTP状态代码(200)表示调用成功.

Having created an OpenWhisk Web Action from the CLI, invoking the action over the public Web Action URL always returns an empty response body. The HTTP status code returned (200) indicating a successful invocation.

无论该函数的返回值如何,空响应主体中均不包含任何内容.

Regardless of the return value from the function, nothing is included in the empty response body.

const fs = require('fs')
const execFile = require('child_process').execFile

function hello(params) {
  return new Promise((resolve, reject) => {
    fs.writeFileSync('test.js', params.code)
    const child = execFile('node', ['test.js'], (error, stdout, stderr) => {
      if (error) {
        console.error('stderr', stderr)
        reject({ payload: error })
      }

      console.log('stdout', stdout)
      resolve({ payload: stdout })
    })
  })

}

exports.hello = hello

该动作是使用以下命令创建的.

The Action was created using the following command.

wsk action create test test.js

使用公共HTTP API调用操作会返回以下响应.

Invoking the Action using the public HTTP API returns the following response.

$ http get "https://openwhisk.ng.bluemix.net/api/v1/web/NAMESPACE/default/test"
HTTP/1.1 200 OK
Connection: Keep-Alive
Date: Thu, 22 Jun 2017 12:39:01 GMT
Server: nginx/1.11.13
Set-Cookie: DPJSESSIONID=PBC5YS:-2098699314; Path=/; Domain=.whisk.ng.bluemix.net
Transfer-Encoding: chunked
X-Backside-Transport: OK OK
X-Global-Transaction-ID: 1659837265

无论从函数返回的值如何,JSON响应主体中都不会包含任何内容.

There is never any content in the JSON response body regardless of the values returned from the function.

推荐答案

网络操作

Web Actions use the body parameter to set the content of the response body. If this value is missing from the object returned by the function, the response body will be blank.

修改您的代码以使用此参数将解决此问题.

Modifying your code to use this parameter will resolve the issue.

const fs = require('fs')
const execFile = require('child_process').execFile

function hello(params) {
  return new Promise((resolve, reject) => {
    fs.writeFileSync('test.js', params.code)
    const child = execFile('node', ['test.js'], (error, stdout, stderr) => {
      if (error) {
        console.error('stderr', stderr)
        reject({ body: error })
      }

      console.log('stdout', stdout)
      resolve({ body: stdout })
    })
  })

}

exports.hello = hello

这篇关于Web Action返回带有空主体的HTTP 200响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆