Ipfs.add() not working

POST https://ipfs.infura.io:5001/v0/add?stream-channels=true&progress=false
while calling ipfs.add() method.

1 Like

Hi @Pon_Logesh sorry to hear you’re having issues, can you provide a more complete snippet of the code you’re using to generate this request and the error response you’re receiving?

Thank you for your reply @mike.
My app.js file code for capturing and submititng file. (bundled using browserify)
sub:function()
{
event.preventDefault();
console.log(“submitted”);
const IPFS=require(‘ipfs-http-client’);
const ipfs=new IPFS({host:‘ipfs.infura.io’,port:5001,protocol:‘https’});
console.log(ipfs);
(async function() {
for await (const file of ipfs.add(buffer))
{
console.log(file);
}
})();
},
change:function(input) {
console.log(“captured file”);
const file=input.files[0];
const reader=new window.FileReader();
reader.readAsArrayBuffer(file);
reader.onload=function(){
buffer=reader.result;
console.log(buffer);
}
}
After submitting file is captured, buffer is displayed but throws a error “Bad Response”
**POST [https://ipfs.infura.io:5001/v0/add?stream-channels=true&progress=false ] .
and also uncaught (in promise) HTTPError: bad request.

Hi @Pon_Logesh it appears the structure of your URL may need to be updated - https://infura.io/docs/ipfs/post/add check out the docs here.

Yours is currently https://ipfs.infura.io:5001/v0/add?params
And it should be https://ipfs.infura.io:5001/api/v0/add?params

Can you try updating that and let me know if it fixes your issue?

Sorry @mike i can’t understand where i want to update this URL because the wrong URL is shown in the chrome console while submitting the form.

Hi @Pon_Logesh what library are you using to generate that IPFS object here,

const IPFS=require(‘ipfs-http-client’);
const ipfs=new IPFS({host:‘ipfs.infura.io’,port:5001,protocol:‘https’});

I am using the javascript ipfs-http-client library recent version.

Awesome, ok, I think if you look here, https://www.npmjs.com/package/ipfs-http-client#importing-the-module-and-usage

Could you try adding the parameter apiPath: '/ipfs/api/v0' ?

Thank You so much @mike.
The code returns the object successfully and file get stored in IPFS when index.html is opened directly.
But the another issues is when i start “npm run dev” in command prompt and try to add file to ipfs it is throwing again bad request

Hey @Pon_Logesh is it possible that your dev configuration doesn’t have the latest app.js with the changes to include the apiPath?

Could you include the full error response?

When I open index.html directly and upload the file, it is added and hash is returned but when i start the npm run dev on cmd and try to upload file it returns a bad request.
My app.js file ipfs add code
async function foo()
{
// Error thrown in this for await call
for await (var file of ipfs.add({path: ‘good’, content: Buffer.from(‘hello’)}))
{
console.log(file);
console.log(file.cid.string);
}
}
foo();
Whether my asynchronous call to add ipfs file is correct?
Error I got for this code while launching npm run dev;
POST https://ipfs.infura.io:5001/ipfs/api/v0/add?stream-channels=true&progress=false 400
Uncaught (in promise) HTTPError: bad request
at async foo (http://localhost:3000/js/bundle.js:52110:19)

Do you have a repo where we can try and run this ourselves?

Ok I think I spotted the error, the npm docs are a bit misleading, can you try
apiPath: '/api/v0'?

Thank You so much @mike .
Now it returns a hash and file is stored in IPFS.

1 Like

Oh, I also met this issue.

//Declare IPFS
const ipfsClient = require('ipfs-http-client')
const ipfs = ipfsClient({ host: 'ipfs.infura.io', port: 5001, protocol: 'https',apiPath: '/ipfs/api/v0' }) 

  //Upload video
  uploadVideo = title => {
    console.log("submit to ipfs")

    ipfs.add(this.state.buffer, (error, result) => {
      console.log("IPFS result: ", result)
      if(error){
        console.log("error: ", error)
        return
      }
    })
  }

when i upload a video,it will no return the hash.

How can I fix it? Thank you very much.

Do I need to run IPFS at local? Do I need to configure Infura about the project in Infura about Ethereum?

Hi @Jeff_Zhu, and welcome to the Infura community!

From your error message, it looks like you’re getting an error when trying to fetch. Have you tried switching your network? There could be something like a firewall blocking access to IPFS. I would try that first. Please let us know if you’re still having the issue and we can dig into it further!

If the firewall isn’t the issue, there are a couple of other easy things to try to fix this error:

  1. check your network panel to make sure you don’t have any other requests that are breaking and causing the error
  2. try adding “http://” at the start of the URL

Looking at the code sample you provided above, I don’t see you calling “fetch” anywhere, which leads me to believe solution #1 above may be your issue. Please give these a try and let us know if they work for you!

Hi, @Leiya_Kenney. Thanks for your reply. I’m in China, so I visit Infura and IPFS by using a VPN.

I’m trying school WIFI and iPhone hotspot to test my project, they all didn’t work. So I think it’s not firewall blocking issue.

I can visit Infura website and IPFS website normal, but I can not visit a detail link about IPFS, for example: https://ipfs.infura.io/ipfs/QmcAkamSRYjKRxaTtyg8Ssshuu7QRsAzYRRQCmWctr8YxN
this link I can’t to access, it’s not response.

For about the #1. I use console.log() to print some prompt information, before running ipfs.add() function, everything is OK.

//Declare IPFS
const ipfsClient = require('ipfs-http-client')
const ipfs = ipfsClient({ host: 'ipfs.infura.io', port: 5001, protocol: 'https' })

For about the #1. I use console.log() to print some tips, I didn't find other requests.

//Declare IPFS
const ipfsClient = require('ipfs-http-client')
const ipfs = ipfsClient({ host: 'ipfs.infura.io', port: 5001, protocol: 'https' })

//Upload video
uploadVideo = title => {
    console.log("submit to ipfs")
    console.log("ipfs: ", ipfs)
    console.log("this.state.buffer: ", this.state.buffer)

    ipfs.add(this.state.buffer, (error, result) => {
      console.log("IPFS result: ", result)
      if(error){
        console.log("error: ", error)
        return
      }
    })
} 

console.log("ipfs: ", ipfs) is running normal, I can get a ipfs object.
this.state.buffer value also is right.

So, I didn’t find the other request causing the error. What other methods can I use to check whether there is a problem with the firewall?

Thank you very much.