使用请求转换节点js的cURL请求
问题描述:
这是一个简单的我猜: 我想请求一个Node.js
服务器,因为我目前正在使用cURL。无论我尝试什么,我都会从服务器收到错误404。使用请求转换节点js的cURL请求
这里是我的合法卷曲命令,它会返回一个JavaScript对象:
curl --data "ajax=1&example=test" http://some-site-example.com
阅读卷曲说明书及申请手册后,这里是我在Node.js
尝试:
request.post({'content-type':'application/x-www-form-urlencoded',ajax:'1',example:'test',url:'http://some-site-example.com'}, function(err, result, body) {
if (err) console.log(err);
else console.log(body);
});
答
你的选择是错误的为application/x-www-form-urlencoded
form。另外,通过指定form
,它会自动设置正确的Content-Type
。所以,试试这个:
request.post({form: {ajax:'1', example:'test'}, url: 'http://some-site-example.com'}, function(err, response, body) {
代替:
request.post({'content-type':'application/x-www-form-urlencoded',ajax:'1',example:'test',url:'http://some-site-example.com'}, function(err, result, body) {
+0
谢谢你的解答和链接 – Thook 2014-10-10 12:44:41
答
这应该这样做,形式自动将内容类型,here's the manual:
request.post('http://some-site-example.com', {form: {ajax:'1',example:'test'}}, function(err, result, body) {
if (err) console.log(err);
else console.log(body);
});
+0
谢谢你的回答 – Thook 2014-10-10 12:45:19
是[要求](HTTPS: //www.npmjs.org/package/request)模块? – Ravi 2014-10-10 12:19:28