【问题标题】:Using node and restler for multipart form-data POST使用 node 和 restler 进行多部分表单数据 POST
【发布时间】:2013-03-10 11:25:15
【问题描述】:

我可以在数据部分使用 restler.file 上传文件,没有问题。我现在正在尝试编写一个非常短的 CSV 数据字符串,我无法找到数据函数的文档,但是阅读了我认为正确的代码:

restler.post("http://posttestserver.com/post.php", {
    multipart: true,
    data: {
            "upload": restler.data("people.csv", "text/csv", '384;213;Status Update'),
            "returnURL": ""
    }
}).on("complete", function(data) {
     console.log(data);
});

不幸的是,这只是挂起并且会超时。我尝试将 EOF 和其他东西添加到第三个参数,但我知道我错过了一些东西。我上面的数据字符串与我使用 restler.file 时工作的文件的内容完全相同。如果我不需要在 POST 之前写出 CSV 文件,我宁愿不必写出来。

【问题讨论】:

  • 我从维护者那里得到了一个注释,说 restler.data 函数也需要一个文件,所以我不知道该去哪里。我采取了稍微不同的策略,但达到了我想要的结果。如果好奇,请参阅下面的答案。
  • 旧restler 代码存在问题,您在示例中所做的restler.data 调用无法正确提交请求。无论如何,在您的原始帖子一年后,这个问题已在github.com/danwrong/restler/pull/172 中修复,现在对我来说很好。

标签: node.js http post multipartform-data restler


【解决方案1】:

编辑----

根据@Joni 对上述问题的评论,在通过pull request 提交修复后,此问题似乎已得到纠正。

原始答案(来自 OP)----

从对 restler 的研究(并与维护者相对应)看来,restler 不能做我想做的事。注意:有人提交了一些代码,允许文件部分以流的形式出现,但它没有被分支接受,而且我对流没有足够的经验。

我解决了回归基础的问题。我阅读了关于 multipart (http://www.ietf.org/rfc/rfc2388.txt) 的 RFC,发现在构建正文时只需要注意一些规则,主要是一些额外的 \r\n 和 '--' 在正确的位置。

我决定简单地格式化原始 POST 正文并通过基本节点 http 客户端发送。

这行得通:

var http = require('http');

postBody = new Buffer(
    '------WebKitFormBoundaryebFz3Q3NHxk7g4qY' + "\r\n" +
    'Content-Disposition: form-data; name="upload"; filename="filename.csv"' + "\r\n" +
    'Content-Type: text/csv' + "\r\n" +
    '\r\n' +
    'comma,separated,values' + "\r\n" +
    '------WebKitFormBoundaryebFz3Q3NHxk7g4qY' + "\r\n" +
    'Content-Disposition: form-data; name="returnUrl"' + "\r\n" + 
    '\r\n' +
    'http://return.url/' + "\r\n" +
    '------WebKitFormBoundaryebFz3Q3NHxk7g4qY--'
    );

var headers = {
  "Content-Type": "multipart/form-data; boundary=----WebKitFormBoundaryebFz3Q3NHxk7g4qY",
  "Content-Length": postBody.length
};

//These are the post options
var options = {
  hostname: 'myhost.com',
  port: 80,
  path: '/myPost',
  method: 'POST',
  headers: headers
};

// so we can see that things look right
console.log("postBody:\n" + postBody);
console.log("postBody.length:\n" + postBody.length);

var responseBody = '';

// set up the request and the callbacks to handle the response data
var request = http.request(options, function(response) {
    // when we receive data, store it in a string
    response.on('data', function (chunk) {
        responseBody += chunk;
    });
    // at end the response, run a function to do something with the response data
    response.on('end',function() {
        console.log(responseBody);
    });
});

// basic error function
request.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

// write our post body to the request
request.write(postBody);
// end the request
request.end();

我希望这可以帮助人们做 multipart/form-data。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 2017-07-02
    • 2021-12-12
    相关资源
    最近更新 更多