Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 可以使用curl发送post请求,但不能从节点服务器发送_Node.js_Web Services_Rest_Curl_Httprequest - Fatal编程技术网

Node.js 可以使用curl发送post请求,但不能从节点服务器发送

Node.js 可以使用curl发送post请求,但不能从节点服务器发送,node.js,web-services,rest,curl,httprequest,Node.js,Web Services,Rest,Curl,Httprequest,我可以使用curl成功地向web服务的REST api端点发出post请求,但使用node.js中的request模块无法发出post请求。相反,我总是得到错误连接ETIMEDOUT。问题是什么 curl命令: curl -i --header "Content-Type: application/json" -XPOST 'http://<endpoint_url>/urls' -d '{ "callback": "http://www.example.com/callback

我可以使用curl成功地向web服务的REST api端点发出post请求,但使用node.js中的request模块无法发出post请求。相反,我总是得到错误
连接ETIMEDOUT
。问题是什么

curl命令:

curl -i --header "Content-Type: application/json" -XPOST 'http://<endpoint_url>/urls' -d '{
  "callback": "http://www.example.com/callback",
  "total": 3,
  "urls": [ {
     "url": "http://www.domain.com/index1.html"
     }, {
     "url": "http://www.domain.com/index2.html"
     }, {
     "url": "http://www.domain.com/index3.html"
     }
  ]
}'
curl-i--header“内容类型:application/json”-XPOST”http:///urls“-d”{
“回调”:http://www.example.com/callback",
“总数”:3,
“URL”:[{
“url”:”http://www.domain.com/index1.html"
}, {
“url”:”http://www.domain.com/index2.html"
}, {
“url”:”http://www.domain.com/index3.html"
}
]
}'
代码:

函数sendRequestToEndPoint(){
常量样本={
“回调”:http://www.example.com/callback",
“总数”:3,
“URL”:[{
“url”:”http://www.domain.com/index1.html"
}, {
“url”:”http://www.domain.com/index2.html"
}, {
“url”:”http://www.domain.com/index3.html"
}
]
}
常量选项={
方法:“post”,
//标题:{
//“内容类型”:“应用程序/json”,
//“接受”:“应用程序/json”,
//},
网址:'http:///urls',
json:示例
//body:JSON.stringify(sample)//还尝试了在
};
控制台日志(示例);
请求(选项,(错误、响应、正文)=>{
console.log(响应)
});
}

更新:原来是因为我使用的api url不正确。

使用querystring来字符串化json数据

var querystring = require('querystring');
...
sample = querystring.stringify(sample);

看看这个答案

使用querystring来字符串化json数据

var querystring = require('querystring');
...
sample = querystring.stringify(sample);
看看这个答案

这个代码有效, 您需要使用json.Stringify对json对象进行字符串化,并使用对象请求的methode write发送示例json对象

, http = require('http')
, bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
 extended: false
}));
var sample = JSON.stringify({
"callback": "http://www.example.com/callback"
, "total": 3
, "urls": [{
        "url": "http://www.domain.com/index1.html"
  }, {
        "url": "http://www.domain.com/index2.html"
  }, {
        "url": "http://www.domain.com/index3.html"
  }
 ]
 });
var options = {
hostname: 'localhost'
, port: 80
, path: '/test/a'
, method: 'POST'
, headers: {
    'Content-Type': 'application/json'
    , 'Content-Length': sample.length
 }
 };
 app.get('/', function (req, res) {
 var r = http.request(options, (response) => {
    console.log(`STATUS: ${response.statusCode}`);
    console.log(`HEADERS: ${JSON.stringify(response.headers)}`);
    response.setEncoding('utf8');
    response.on('data', (chunk) => {
        console.log(`BODY: ${chunk}`);
    });
    response.on('end', () => {
        console.log('No more data in response.');
    });
 });
 r.on('error', (e) => {
    console.log(`problem with request: ${e.message}`);
 });
 r.write(sample);
 r.end();
 res.send('ok');
});
有关http.request的更多详细信息的链接, 您需要使用json.Stringify对json对象进行字符串化,并使用对象请求的methode write发送示例json对象

, http = require('http')
, bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
 extended: false
}));
var sample = JSON.stringify({
"callback": "http://www.example.com/callback"
, "total": 3
, "urls": [{
        "url": "http://www.domain.com/index1.html"
  }, {
        "url": "http://www.domain.com/index2.html"
  }, {
        "url": "http://www.domain.com/index3.html"
  }
 ]
 });
var options = {
hostname: 'localhost'
, port: 80
, path: '/test/a'
, method: 'POST'
, headers: {
    'Content-Type': 'application/json'
    , 'Content-Length': sample.length
 }
 };
 app.get('/', function (req, res) {
 var r = http.request(options, (response) => {
    console.log(`STATUS: ${response.statusCode}`);
    console.log(`HEADERS: ${JSON.stringify(response.headers)}`);
    response.setEncoding('utf8');
    response.on('data', (chunk) => {
        console.log(`BODY: ${chunk}`);
    });
    response.on('end', () => {
        console.log('No more data in response.');
    });
 });
 r.on('error', (e) => {
    console.log(`problem with request: ${e.message}`);
 });
 r.write(sample);
 r.end();
 res.send('ok');
});

有关http.request的更多详细信息的链接你好,我尝试在
sample
上使用
querystring
,它将
sample
转到
callback=http%3A%2F%2Fwww.example.com%2Fcallback&total=3&url=&url=&url=&url=
。另外,我认为端点需要
application/json
,因此可能没有必要使用
querystring
?嗨,我尝试在
sample
上使用
querystring
,它将
sample
转换为
callback=http%3A%2F%2Fwww.example.com%2Fcallback&total=3&url=&url=&url=&url=
。另外,我认为端点需要
application/json
,所以可能不需要
querystring