【问题标题】:Web Action returns HTTP 200 response with empty bodyWeb 操作返回带有空正文的 HTTP 200 响应
【发布时间】:2017-11-25 18:09:33
【问题描述】:

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

【问题讨论】:

    标签: ibm-cloud openwhisk


    【解决方案1】:

    Web Actions use the body parameter 设置响应正文的内容。如果函数返回的对象中缺少此值,则响应正文将为空白。

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

    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
    

    【讨论】:

      猜你喜欢
      • 2021-04-06
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2015-03-11
      相关资源
      最近更新 更多