2014-10-10 160 views
0

这是一个简单的我猜: 我想请求一个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); 
}); 
+0

是[要求](HTTPS: //www.npmjs.org/package/request)模块? – Ravi 2014-10-10 12:19:28

回答

1

你的选择是错误的为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

0

这应该这样做,形式自动将内容类型,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