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

Time:03-02

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.

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

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();
  • Related