[英]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.