Search code examples
ibm-cloudopenwhisk

Web Action returns HTTP 200 response with empty body


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.


Solution

  • 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