Ipfs.add() not working

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

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