Node.js 使用请求转换节点js的cURL请求

Node.js 使用请求转换节点js的cURL请求,node.js,curl,request,Node.js,Curl,Request,我想这是一个简单的例子: 我想在Node.js服务器上发出请求,因为我目前正在使用cURL。无论我尝试什么,我都会从服务器得到一个错误404 下面是我的有效cURL命令,它返回一个JavaScript对象: curl --data "ajax=1&example=test" http://some-site-example.com 在阅读了cURL手册和Request手册之后,下面是我在Node.js中尝试的内容: request.post({'content-type':'appli

我想这是一个简单的例子: 我想在
Node.js
服务器上发出请求,因为我目前正在使用cURL。无论我尝试什么,我都会从服务器得到一个错误404

下面是我的有效cURL命令,它返回一个JavaScript对象:

curl --data "ajax=1&example=test" http://some-site-example.com
在阅读了cURL手册和Request手册之后,下面是我在
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);
});

你的选择是错误的。另外,通过指定
表单
,它会自动设置正确的
内容类型
。所以试试这个:

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) {

这样,表单会自动添加内容类型:


是模块吗?感谢您提供的解决方案和链接谢谢您的回答
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);
});