Javascript 如何将此curl请求转换为node.js请求?
我有这个curl请求,我希望将其转换为node.js代码Javascript 如何将此curl请求转换为node.js请求?,javascript,php,node.js,curl,Javascript,Php,Node.js,Curl,我有这个curl请求,我希望将其转换为node.js代码 curl -H "Content-Type: application/json" -X POST -d '{"text":"ibm","date":{"from":"20170330","to":"20170530"},"restrictedDateRange":false}' https://finsights.mybluemix.net/api/query 然而,我尝试了我的方式,但我很确定我做错了什么,因为响应主体与我从curl请
curl -H "Content-Type: application/json" -X POST -d '{"text":"ibm","date":{"from":"20170330","to":"20170530"},"restrictedDateRange":false}' https://finsights.mybluemix.net/api/query
然而,我尝试了我的方式,但我很确定我做错了什么,因为响应主体与我从curl请求中得到的不匹配
我失败的代码:
server.get('/hello', function create(req, res, next) {
// //sample request
request({
url: "https://finsights.mybluemix.net/api/query",
method: "POST",
headers: {
"content-type": "application/json",
data: {
"text": "ibm",
"date": {
"from": "20170330",
"to": "20170530"
},
"restrictedDateRange": "false",
}
},
json: true, // <--Very important!!!
},
function(err, res, body) {
console.log(body);
});
// //end of sample request
console.log("success");
return next();
});
server.get('/hello',函数create(req,res,next){
////请求示例
请求({
url:“https://finsights.mybluemix.net/api/query",
方法:“张贴”,
标题:{
“内容类型”:“应用程序/json”,
数据:{
“文本”:“ibm”,
“日期”:{
“发件人”:“20170330”,
“致”:“20170530”
},
“RestrictedDaterRange”:“false”,
}
},
json:true,//您的数据不应该是头。请尝试将数据包含在请求正文中,如下所示:
request({
url: "https://finsights.mybluemix.net/api/query",
method: "POST",
headers: {
"content-type": "application/json"
},
body: {
"text": "ibm",
"date": {
"from": "20170330",
"to": "20170530"
},
"restrictedDateRange": "false",
},
json: true
},
function(err, res, body) {
console.log(body);
});
对于“RestrictedDaterRange”,您发送了字符串
“false”,请尝试使用布尔值来匹配卷曲。我认为您的数据不应该是标题。请尝试这样设置您的请求-嘿,伙计,我真的很感谢,它很有效,非常感谢!