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?
JavaScript
x
15
15
1
const client = create({
2
url: ipfsUrl(),
3
headers: {
4
authorization: ipfsAuthPhrase(),
5
},
6
});
7
const cidformat = "f" + cid.substring(2);
8
const cidV0 = new CID(cidformat).toV0().toString();
9
const resp = await client.cat(cidV0);
10
let content = [];
11
for await (const chunk of resp) {
12
content = [content, chunk];
13
}
14
console.log(content.toString());
15
right now i am just getting a array of binaries on the console log.
Advertisement
Answer
From this point on its just a matter of decoding the content
buffer.
If the content is some JSON:
JavaScript
1
3
1
const raw = Buffer.from(content).toString('utf8')
2
console.log(JSON.parse(raw))
3
If the content is an image:
JavaScript
1
2
1
Buffer.from(content).toString('base64')
2