'What kind of encoding is this and how to convert it into readable stream in nodejs to save?

enter image description here

I am getting this string response from a REST API call. It is a file but it's in string. I am stuck on how to convert it into the readable stream so I can save it into the system.



Solution 1:[1]

Solved it using node-fetch and on response.body() it's returning a readable stream.

Solution 2:[2]

Based on the presence of EXIF and XMP data, you're looking at an image.

You can't process it as a (human-readable-text) string, so there is no "encoding" to work with. Just store it as binary.

Solution 3:[3]

This might not be what you want, but I believe it answers the second part of your question:

It could be different depending on which HTTP client you are using, but if you use the node http or https module, then the response will be a readable stream by default.

import https from "https";

const req = https.request(
  "https://example.com",
  {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
  },
  (res) => {
    /* res extends <stream.Readable> */
  }
);
const postData = JSON.stringify({ msg: "hello world" });
req.write(postData);
req.end();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 AKX
Solution 3