profile
viewpoint

Ask questionsUsing superagent as a writable stream

Hi there,

I am trying to upload some files to OpenStack ObjectStorage, creating a read stream from a file and piping into request.

var request = require('superagent');

var req = request.post(base);
var stream = fs.createReadStream('/path/to/file');

req.on('response', function(res) {
  console.log('Success');
});

stream.pipe(req);

The above works for text files, but for images nothing happens and the request times out.

Update: I added listeners for the 'error' event on both readable and writable streams but there is nothing

Using .attach() "works" but the uploaded file now has all the multipart form metadata i.e. the image is "corrupted".

Versions

Node v4.8.3 Superagent 3.5.2

Any ideas? I feel like I am missing something obvious here, thanks in advance 😄

visionmedia/superagent

Answer questions contra

Yep this is still an issue ^

FWIW I have a workaround that seems to be working as expected:

new Promise((resolve, reject) => {
    const upload = request.post(url)
    const req = upload.request()
    pipeline(stream, req, (err) => {
      if (err) {
        upload.abort()
        return reject(err)
      }

      upload.end((err, res) => {
        if (err) return reject(err)
        return resolve(res.body)
      })
    })
  })
useful!
source:https://uonfu.com/
answerer
contra contra @staeco Phoenix, AZ https://contra.io Engineer/founder - @staeco • @gulpjs • @wearefractal • @genomejs + more
Github User Rank List