0

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 - http / https
  • domain - ipfs.infura.io / gateway.ipfs.io / localhost:5555
  • path - ipfs / ipns
  • hash - hash from Response

{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?

Narnik Gamarnik
  • 1,049
  • 1
  • 16
  • 35
  • I'm not sure of why that doesn't works but you may not try to add file like that, IPFS first broke file in multiple parts (DAG linked) with IPLD, try to use ipfs API client (https://github.com/ipfs/js-ipfs-http-client). – Jorropo Jan 03 '20 at 01:11

1 Answers1

2

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

lidel
  • 525
  • 3
  • 7