How to get file contents from ipfs-http-client

3k views Asked by At

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.

1

There are 1 answers

2
get_php_workin On BEST ANSWER

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')