1

从 CLI 创建 OpenWhisk Web 操作后,通过公共 Web 操作 URL 调用操作始终返回空响应正文。返回的 HTTP 状态代码 (200) 表示调用成功。

无论函数的返回值如何,空响应正文中都不包含任何内容。

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

操作是使用以下命令创建的。

wsk action create test test.js

使用公共 HTTP API 调用 Action 会返回以下响应。

$ 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 响应正文中都没有任何内容。

4

1 回答 1

1

Web Actions使用该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({ body: error })
      }

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

}

exports.hello = hello
于 2017-06-22T12:44:50.333 回答