Node.js 可以使用curl发送post请求,但不能从节点服务器发送
我可以使用curl成功地向web服务的REST api端点发出post请求,但使用node.js中的request模块无法发出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
连接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
?