3

I am doing some research and testing based on the js-ipfs-http-client browser example here

When I try to get a response from IPFS, I receive the following warning from the firefox console:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5001/api/v0/add?wrapWithDirectory=true&progress=true&wrap-with-directory=true&stream-channels=true. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

I already tried the recommended (dirty) fix where you change your IPFS config from the terminal:

ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\"*\"]"

But not even this seems to work. I started looking into possibly using custom headers as mentioned here

No luck.

I set up IPFS like so: const ipfsClient = require('ipfs-http-client'); var ipfs = ipfsClient('localhost', '5001');

And then once i get some file from the user, I attempt to save it to IPFS like so:

function saveToIpfsWithFilename (file) 
{
    console.log('running save');

    let ipfsId;
    const fileStream = fileReaderPullStream(file);
    const fileDetails = 
    {
      path: file.name,
      content: fileStream
    };

    const options =
    {
      wrapWithDirectory: true,
      progress: (prog) => console.log(`received: ${prog}`)
    };

    ipfs.add(fileDetails, options).then((response) => 
    {
        console.log(response)
        // CID of wrapping directory is returned last
        ipfsId = response[response.length - 1].hash
        console.log(ipfsId)
    }).catch((err) => {
        console.error(err)
      });
}
John Doe
  • 108
  • 12
  • Hm, I'm getting errors trying to run the example in order to test this. Reported the issue, and will check back here once I get it working. – Dietrich Ayala Apr 12 '19 at 22:35
  • Ok, fixed the example [1] and made both config changes noted in the documentation [2] and it's working for me. I'm running Firefox Nightly 68 and IPFS Desktop 0.7.2 which has go-ipfs 0.4.19. (1. https://github.com/ipfs/js-ipfs-http-client/pull/970 2. https://github.com/ipfs/js-ipfs-http-client/tree/master/examples/upload-file-via-browser ) – Dietrich Ayala Apr 15 '19 at 18:19

1 Answers1

1

I have also faced same issue then I try to run these following commands and that works for me

ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/8080
ipfs config Addresses.API /ip4/0.0.0.0/tcp/5001
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\"*\"]"
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials "[\"true\"]"

hope this will solve your problem

pranjal85
  • 26
  • 5