Web Action returns HTTP 200 response with empty body

99封情书 提交于 2019-12-11 16:42:45

问题


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

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

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


回答1:


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


来源:https://stackoverflow.com/questions/44699797/web-action-returns-http-200-response-with-empty-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!