0

データセクションの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);
});

残念ながら、これはハングし、タイムアウトになります。3番目の引数にEOFなどを追加しようとしましたが、何かが足りないことがわかりました。上記のデータ文字列は、restler.fileを使用したときに機能するファイルとまったく同じ内容です。投稿する前にCSVファイルを書き出す必要がなければ、書き出す必要はありません。

4

1 に答える 1

5

編集 - -

上記の質問に対する@Joniのコメントによると、この問題は、プルリクエストを介して修正が送信された後に修正されたようです。

元の回答(OPから)----

Restler(およびメンテナに対応)に関する調査から、Restlerが私が望んでいたことを実行できるようには見えません。注:誰かがストリームの形式でファイル部分を許可するコードをコミットしましたが、それはブランチに受け入れられておらず、私はストリームに関する十分な経験がありません。

基本に戻って問題を解決しました。マルチパートのRFC(http://www.ietf.org/rfc/rfc2388.txt)を読んだところ、本文を作成する際に知っておくべきルールはごくわずかであり、ほとんどの場合、追加の\ 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を行う人々に役立つことを願っています。

于 2013-03-21T21:56:19.540 に答える