How to buffer an HTTP response using the request module?

前端 未结 4 841
清酒与你
清酒与你 2021-02-01 18:52

I would like to stream the contents of an HTTP response to a variable. My goal is to get an image via request(), and store it in in MongoDB - but the image is alway

4条回答
  •  青春惊慌失措
    2021-02-01 19:39

    Nowadays, you can easily retreive a file in binary with Node 8, RequestJS and async await. I used the following:

    const buffer = await request.get(pdf.url, { encoding: null }); 
    

    The response was a Buffer containing the bytes of the pdf. Much cleaner than big option objects and old skool callbacks.

提交回复
热议问题