question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using superagent as a writable stream

See original GitHub issue

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 😄

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jmarcacommented, Aug 10, 2017

Perhaps my issue is related.

I’m trying to stream an image into CouchDB. It works fine with Request, but fails with superagent.

I wrote a test and put it into node/pipe.js as follows:

  it('should act as a writable stream for images', function(done){
    var req = request.post(base);
    var stream = fs.createReadStream('test/node/fixtures/plot.png');
      stream.on('data',(chunk)=>{
          console.log(`Received ${chunk.length} bytes of data.`);
      })
      req.type('png');
      req.accept('png');
      req.on('response', function(res){
          console.log('got response');
          done();
    });

    stream.pipe(req);
  })

It hangs after reading just two chunks. Same behavior on my app (reads two chunks and hangs)

james@emma superagent[bug/stream_image]$ ./node_modules/.bin/mocha  test/node/pipe.js  --timeout 5000


  request pipe
    1) should act as a writable stream
Received 65536 bytes of data.
Received 65536 bytes of data.
    2) should act as a writable stream for images
    3) should act as a readable stream

node version 7.10.0

attached is the plot file I’m using plot

0reactions
yocontracommented, Jul 2, 2021

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)
      })
    })
  })
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using superagent as a writable stream · Issue #1250 - GitHub
I am trying to upload some files to OpenStack ObjectStorage, creating a read stream from a file and piping into request. var request...
Read more >
Piping readable stream using superagent - node.js
Create a superagent instance with URL, method and HTTP headers you want for uploading,; Listen to data events on the incoming file stream,...
Read more >
superagent.SuperAgentRequest.pipe JavaScript and Node.js ...
Best JavaScript code snippets using superagent. ... imgsDir + fileName); let req = request.get(img); let stream = req.pipe(write); logger.debug('>>>> start ...
Read more >
readable-stream - npm.io
This package is a mirror of the streams implementations in Node.js 18.9.0. Full documentation may be found on the Node.js website. If you...
Read more >
superagent - Bountysource
var request = require('superagent'); var req = request.post(base); var stream = fs.createReadStream('/path/to/file'); req.on('response', function(res) { console ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found