使用node和restler进行多部分表单数据POST [英] Using node and restler for multipart form-data POST

查看:399
本文介绍了使用node和restler进行多部分表单数据POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am able to upload a file using restler.file in the data section with no problem. I am now trying to write a very short CSV data string, which I am not able to find documentation for the data function, but reading the code I thought I had it correct:

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和其他东西添加到第3个arg,但我知道我错过了一些东西。我上面的数据字符串与使用restler.file时的文件完全相同。如果在发布之前没有必要,我宁愿不写出CSV文件。

Unfortunately this just hangs and will time out. I tried adding EOF and other things to the 3rd arg but I know I am missing something. The data string I have above is the exact same contents as the file that works when I use restler.file. I would rather not have to write out a CSV file if I don't have to before POSTing it.

推荐答案

编辑----

根据@Joni对上述问题的评论,在通过拉取请求

As per @Joni's comment to the question above, this problem seems to have been rectified after a fix was submitted via pull request.

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

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

From the research on restler (and corresponding with the maintainer) it doesn't look like restler can do what I wanted. Note: Someone has committed some code that would allow a file part in the form of a stream, but it hasn't been accepted into the branch and I don't have enough experience with streams.

我解决了回归基础的问题。我阅读了RFC for multipart( http://www.ietf.org/rfc/rfc2388.txt )并且发现在构建身体时只需要注意一些规则,大多数在正确的位置有一些额外的\\ n和' - '。

I solved the problem going back to basics. I read the RFC for multipart (http://www.ietf.org/rfc/rfc2388.txt) and found there are only a few rules to be aware of in constructing the body, mostly some extra \r\n's and '--' in the right places.

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

I decided to simply format the raw POST body and send it through the basic node http client.

这个工作:

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();

我希望这可以帮助人们做多部分/表格数据。

I hope this helps people doing multipart/form-data.

这篇关于使用node和restler进行多部分表单数据POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆