简体   繁体   English

如何在嵌入式编辑器dialogflow中发出发布请求?

[英]how to make post request in inline editor dialogflow?

First of all,I am using the blaze tier, so no issue of billing. 首先,我使用的是blaze层,因此没有计费问题。 I have also included 我也包括了
"request" : "*" “ request”:“ *”
in package.json dependencies. 在package.json依赖项中。 Look at my code index.js in the inline editor: 在内联编辑器中查看我的代码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);
            }
        }
    );

` `

But in the firebase log I am getting the following error: 但是在firebase日志中,出现以下错误:

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

I followed How to make an HTTP POST request in node.js? 我遵循了如何在node.js中发出HTTP POST请求? for help. 求助。 But the code still has errors. 但是代码仍然有错误。 What am I doing wrong here?? 我在这里做错了什么? Thanks for help. 感谢帮助。

The Dialogflow library assumes that you're using Promises if you're doing asynchronous operations. 如果您执行异步操作,则Dialogflow库假定您正在使用Promises。

Typically, instead of using the request library, I use the request-promise-native library. 通常,我使用request-promise-native库而不是使用request库。 So that block of code might look something like this: 因此该代码块可能看起来像这样:

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 );
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在Dialogflow内联编辑器中发出HTTP请求 - Unable to make http request from within Dialogflow inline editor Dialogflow 实现内联编辑器 api 请求 - Dialogflow fulfillment inline editor api request 在DialogFlow内联编辑器中使用“ request-promise-native”包 - Use 'request-promise-native' package in DialogFlow inline editor 内联编辑器中的对话流捕获实体 - dialogflow capturing entity in inline editor 如何在 Google Dialogflow Fullfilment Inline Editor 中安装 npm 包 - How to install npm packages within Google Dialogflow Fullfilment Inline Editor 如何使用DialogFlow在线编辑器从Firebase检索数据 - How to retrieve data from Firebase using DialogFlow Inline Editor 是否可以在dialogflow内联编辑器中使用外部库? - Is it possible to use external libraries in the dialogflow inline editor? 如何在环境Dialogflow->实现中发出http请求? - How do I make a http request in the environment Dialogflow -> Fulfillment? Dialogflow 到 Firestore 内联编辑器 javascript:对集合的不同参数进行排序和过滤 - Dialogflow to firestore inline editor javascript: sorting and filtering on different parameters of collection 使用对话框流在内联编辑器之外创建不可知的卡片 - Creating agnostic cards outside inline editor using dialogflow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM