【问题标题】:how to make post request in inline editor dialogflow?如何在内联编辑器对话框流​​中发出发布请求?
【发布时间】:2018-06-07 14:35:26
【问题描述】:

首先,我使用的是 blaze 层,所以没有计费问题。 我也包括了
“请求”:“*”
在 package.json 依赖项中。 在内联编辑器中查看我的代码 index.js:

`

'use strict';
var global_request = require('request');
var myJSONObject = {
        "limit": 10,
        "offset": 0,
        "query": "example"
    };
global_request.post(
    'https://example.com', { 
        json: myJSONObject },
        function (error, res, body) {
            if (!error && res.statusCode == 200) {
                console.log(res);
                console.log(body);
            }
        }
    );

`

但在 firebase 日志中我收到以下错误:

Unhandled rejection
Error: Can't set headers after they are sent.

我关注How to make an HTTP POST request in node.js? 寻求帮助。但是代码仍然有错误。 我在这里做错了什么?感谢帮助。

【问题讨论】:

    标签: javascript node.js dialogflow-es fulfillment


    【解决方案1】:

    如果您进行异步操作,Dialogflow 库假定您使用的是 Promises。

    通常,我不使用request 库,而是使用request-promise-native 库。所以这段代码可能看起来像这样:

    var rp = require('request-promise-native');
    var myJSONObject = {
            "limit": 10,
            "offset": 0,
            "query": "example"
        };
    var options = {
      method: 'post',
      uri: 'https://example.com',
      body: myJSONObject,
      json: true
    };
    return rp( options )
      .then( body => {
        console.log( body );
        // This is also where you'd call the library
        // to send a reply.
        return Promise.resolve( true );
      })
      .catch( err => {
        // You should also return a message here of some sort
        return Promise.resolve( true );
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-25
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-13
      相关资源
      最近更新 更多