IPFS Access – How to Access IPFS via Port 5001

ipfs

I am using IPFS to store media files with the address on the blockchain. I have had a lot of issues with ports and access. In the code below I have some code partly running but I get an access issue. I think this is related to the API as the IP is set to 0.0.0.0.

Error –

POST http://162.243.237.41:5001/api/v0/add?stream-channels=true 403   (Forbidden)
ClientRequest._onFinish @ request.js:130
(anonymous) @ request.js:62
EventEmitter.emit @ events.js:96
finishMaybe @ _stream_writable.js:504
endWritable @ _stream_writable.js:514
Writable.end @ _stream_writable.js:484
ClientRequest.end @ request.js:273
onend @ _stream_readable.js:499
g @ events.js:165
EventEmitter.emit @ events.js:78
endReadableNT @ _stream_readable.js:920
afterTickTwo @ index.js:27
Item.run @ browser.js:153
drainQueue @ browser.js:123
jenbil.com/:1 Fetch API cannot load http://162.243.237.41:5001/api/v0/add?stream-channels=true. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://jenbil.com:3000' is therefore not allowed access. The response had HTTP status code 403. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Code (ReactJS) in class App extends Component { –

 this.IpfsAPI = IpfsAPI('162.243.237.41', '5001')

Code in render –

var zstr = 'hello world from Zillerium2'
   this.IpfsAPI.add(new Buffer(zstr), function (err, res){
          console.log("hello");
          if(err || !res) return console.error("ipfs add error", err, res);
          else{
            console.log("no issue");
            console.log(res);
            res.forEach(function(text) {
                   console.log('successfully stored', text.hash);
                 //  console.log('successfully stored', text.path);
                 //  display(file.Hash);
                    var textaddress=text.hash;
                    console.log(textaddress);
            });
          }
        });

I have the IPFS deamon as follows –

root@ubuntu-2gb-nyc2-01:/home/zipfs# ipfs daemon
Initializing daemon...
Adjusting current ulimit to 2048...
Successfully raised file descriptor limit to 2048.
Swarm listening on /ip4/10.13.0.5/tcp/4001
Swarm listening on /ip4/127.0.0.1/tcp/4001
Swarm listening on /ip4/162.243.237.41/tcp/4001
Swarm listening on /ip4/162.243.237.41/tcp/4001
Swarm listening on /ip6/::1/tcp/4001
API server listening on /ip4/0.0.0.0/tcp/5001
Gateway (readonly) server listening on /ip4/0.0.0.0/tcp/8180
Daemon is ready

Is the API call assigned to this.IpsfAPI wrong? The IP address looks ok.

Best Answer

IPFS CORS needs to be enabled to connect and access through JavaScript Api

$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\"http://example.com\"]"
$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials "[\"true\"]"
$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\"PUT\", \"POST\", \"GET\"]"

for reference

https://github.com/ipfs/js-ipfs-api#cors

And your remote machine, the firewall needs to be opened for the requesting port.

If still it is not connecting means try to connect with multiaddr of the same ipfs daemon.

Related Topic