【问题标题】:Converting cURL request for node js using Request使用请求转换节点 js 的 cURL 请求
【发布时间】:2014-10-10 12:08:08
【问题描述】:

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

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

curl --data "ajax=1&example=test" http://some-site-example.com

阅读cURL手册和请求手册后,这是我在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);
});

【问题讨论】:

标签: node.js curl request


【解决方案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) {

【讨论】:

  • 感谢您的解决方案和链接
【解决方案2】:

这样就可以了,form自动添加content-type,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);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    相关资源
    最近更新 更多