not immediately request #466

Closed
Luavis opened this Issue Mar 8, 2013 · 5 comments

Projects

None yet

3 participants

@Luavis
Luavis commented Mar 8, 2013

Because I have to calculate node-form-datas size by getLength() function,request` have to wait for callback.

is there any wait to pause

p.s.

i find pause function in request. so i use it and add resume function in callback but it dosen`t work......ㅠㅠ;;

@mikeal
Member
mikeal commented Mar 8, 2013

pause() in pre node-0.10 doesn't actually insure that data won't emit. could you attach some example code, i'm having a hard time following exactly what you're asking.

@Luavis
Luavis commented Mar 8, 2013
var req = request.post(disk_url + '/HtmlUploadService.do',
            {
                method : 'POST',
                jar : diskCore.j
            },function(err, res, body) { console.log(body); }
        );
        // here i wnat to pause

        var f = req.form();

        f.append('pid', diskId);
        f.append('type', 'html');
        f.append('file1', fs.createReadStream('./node.mp3'));

        f.geLength(function(err, len){
            req.setHeader('Content-Length', len);
            //when i have to send data
        });
@very
very commented Mar 26, 2013

I guess the real problem here is that you cannot create a FormData first and the Request afterwards. Even if you would require the form-data library yourself, you couldn't use those FormData objects with the current request API. Both the form option and the form method treat supplied objects as simple key-value maps. So there is no official way for mixing FormData objects with asynchronicity.

Either that, or the problem is that there is no way to prepare a Request without having it started in the next tick.

@mikeal
Member
mikeal commented Mar 26, 2013

we could add an option to request that takes a form-data object rather than create a new one. would that work?

On Mar 25, 2013, at 7:43PM, very notifications@github.com wrote:

I guess the real problem here is that you cannot create a FormData first and the Request afterwards. Even if you would require the form-data library yourself, you couldn't use those FormData objects with the current request API. Both the form option and the form method treat supplied objects as simple key-value maps. So there is no official way for mixing FormData objects with asynchronicity.


Reply to this email directly or view it on GitHub.

@very
very commented Mar 26, 2013

Yes, I would think so.

The current hack is to set Request#_form directly:

var request = require('request'),
    FormData = require('form-data'),
    fs = require('fs');

var form = new FormData();
form.append('file', fs.createReadStream(file));
form.getLength(function(err, length) {
    if (err) return callback(err);
    request.post({
        url: url,
        headers: {
            'content-length': length
        },
        callback: callback
    })._form = form;
});

function callback(err) {
    if (err) console.log(err);
}

If you would add an option/method or modify the current form option/method it would look a bit cleaner:

var request = require('request'),
    FormData = require('form-data'),
    fs = require('fs');

var form = new FormData();
form.append('file', fs.createReadStream(file));
form.getLength(function(err, length) {
    if (err) return callback(err);
    request.post({
        url: url,
        form: form,
        headers: {
            'content-length': length
        },
        callback: callback
    });
});

function callback(err) {
    if (err) console.log(err);
}
@Luavis Luavis closed this Aug 23, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment