'How to get file contents from ipfs-http-client

I am using ipfs-http-client to read the contents of a file form infura, how do i use the "cat" funtionality to correctly get the data in a string/json format?

 const client = create({
    url: ipfsUrl(),
    headers: {
      authorization: ipfsAuthPhrase(),
    },
  });
  const cidformat = "f" + cid.substring(2);
  const cidV0 = new CID(cidformat).toV0().toString();
  const resp = await client.cat(cidV0);
  let content = [];
  for await (const chunk of resp) {
    content = [...content, ...chunk];
  }
  console.log(content.toString());

right now i am just getting a array of binaries on the console log.



Solution 1:[1]

From this point on its just a matter of decoding the content buffer.

If the content is some JSON:

const raw = Buffer.from(content).toString('utf8')
console.log(JSON.parse(raw))

If the content is an image:

Buffer.from(content).toString('base64')

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 get_php_workin