Narnik Gamarnik
Narnik Gamarnik

Reputation: 1111

IPFS. DAG get content from web browser

I send request to IPFS through HTTP Client:

var cleanScript = {
    'type': 'script'
};
var formData = new FormData(); 
var jsonse = JSON.stringify(cleanScript);
var blob = new Blob([jsonse], {type: "application/json"});
formData.append('file', blob, 'file.json')
            
fetch('https://ipfs.infura.io:5001/api/v0/add', {
	method: 'POST',
	body: formData
})
.then(r => r.json())
.then(data => console.log(data))

And I can get access to this stuff through the browser, for example:

https://ipfs.infura.io/ipfs/QmZp5tQwLkMxpYHHK4a1989xYCjfUG81Po7LoaUwmxpDqP https://gateway.ipfs.io/ipfs/QmZp5tQwLkMxpYHHK4a1989xYCjfUG81Po7LoaUwmxpDqP

A link is formed by the following principle:

{protocol}://{domain}/{path}/{hash}

But if I work with DAG:

var cleanScript = {
    "a": 1,
    "b": [1, 2, 3],
    "c": {
        "ca": [5, 6, 7],
        "cb": "foo"
    }
};
var formData = new FormData();
var jsonse = JSON.stringify(cleanScript);
var blob = new Blob([jsonse], {
    type: "application/json"
});
formData.append('file', blob, 'somefile.json')

fetch('https://ipfs.infura.io:5001/api/v0/dag/put', {
        method: 'POST',
        body: formData
    })
    .then(r => r.json())
    .then(data => console.log(data))

I don’t understand how the link is formed.

Can I access content through a browser?

Upvotes: 0

Views: 1139

Answers (1)

lidel
lidel

Reputation: 555

HTTP Gateway provided by go-ipfs v0.4.22 supports returning files and directories only. Those are DAGs in unixfsv1 format (identified with dag-pb multicodec). You can see dag-pb being a part of your first CID at cid.ipfs.io.

The custom DAGs you create via /api/v0/dag/put are added as dag-cbor by default, and you can't read them over HTTP Gateway, because they are no longer a file or a directory.

You should be able to read them over HTTP API endpoint at /api/v0/get. For example:

https://ipfs.io/api/v0/dag/get?arg=bafyreiah7uhzdxbuik6sexirej22iyi5nau3d4nnfhv6ux33ogtdpeznpm

Upvotes: 2

Related Questions