簡體   English   中英

如何在嵌入式編輯器dialogflow中發出發布請求?

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

首先,我使用的是blaze層,因此沒有計費問題。 我也包括了
“ request”:“ *”
在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.

我遵循了如何在node.js中發出HTTP POST請求? 求助。 但是代碼仍然有錯誤。 我在這里做錯了什么? 感謝幫助。

如果您執行異步操作,則Dialogflow庫假定您正在使用Promises。

通常,我使用request-promise-native庫而不是使用request庫。 因此該代碼塊可能看起來像這樣:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM