简体   繁体   中英

how to download a file from given url to buffer with got library?

I want to download a file from an url with got library, this is my current code:

    var data
    try
    {
        var stream = got.stream (url)
        stream.on ('data', d => data += d)
        stream.end ()
    }
    catch (error)
    {
        console.error (error)
    }

But i'm getting this error:

TypeError: The payload has been already provided

got.stream() makes a GET request by default, you need to set allGetBody to true to avoid the TypeError .

So do this instead:

var data
try {
  var stream = got.stream (url, { allowGetBody: true })
  stream.on ('data', d => data += d);
  stream.end()
} catch (error) {
  console.error (error)
}

See Got Documentation on this

Here's a working example on Javascript Runkit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM