0

I'm using IPFS service for file storage. From Node.js application I can initialize IPFS_Node and using this Node upload file.

var ipfs = ipfsClient({
            // the hostname (or ip address) of the endpoint providing the ipfs api
            host: '*****************',
            // the port to connect on
            port: '443',
            // 'api-path': '/api/v0/',
            // the protocol, https for security
            protocol: 'https',
            // provide the jwt within an authorization header
            headers: {
                authorization:
                    'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ**********************'
            }
        });
    for await (const response of ipfs.add(file.buffer)) {
            console.log(response);
            res.json({ response: response, status: 200 });
        }

Ipfs add throws Error Response

HTTPError: Unauthorized

   at Object.errorHandler [as handleError] (C:\Users\Rohail\Documents\GitHub\Artbot_api\node_modules\ipfs-http-client\src\lib\core.js:67:15)        
   at processTicksAndRejections (internal/process/task_queues.js:93:5)
   at Client.fetch (C:\Users\Rohail\Documents\GitHub\Artbot_api\node_modules\ipfs-utils\src\http.js:145:9)
   at Object.add (C:\Users\Rohail\Documents\GitHub\Artbot_api\node_modules\ipfs-http-client\src\add.js:13:17) {
 name: 'HTTPError',
 response: Response {
   size: 0,
   timeout: 0,
   [Symbol(Body internals)]: { body: [PassThrough], disturbed: true, error: null },
   [Symbol(Response internals)]: {
     url: 'https://api.ipfs.temporal.cloud/api/v0/add?stream-channels=true&progress=false',
     status: 401,
     statusText: 'Unauthorized',
     headers: [Headers],
     counter: 0
   }
 }
}
Rohail Butt
  • 421
  • 1
  • 7
  • 22

2 Answers2

0

Not sure if this helps, but if you are using latest version of ipfs-http-client, then ipfs.add returns AsyncIterable<Object> instead of Promise now.

Try something like:

const all = require('it-all')
const data = await all(ipfs.add(file))
console.log(data)

For more info, see: https://blog.ipfs.io/2020-02-01-async-await-refactor/

lidel
  • 525
  • 3
  • 7
  • It should work in both browser and nodejs. If you've found a bug, please report details at https://github.com/ipfs/js-ipfs/issues/new/choose – lidel May 31 '20 at 16:14
0

I have found an error the issue is ipfs-http-client version (^44.0.1). When I update it into the latest version (^44.1.1) error resolved.

Change this: "ipfs-http-client": "^44.0.1"

To: "ipfs-http-client": "^44.1.1",

Rohail Butt
  • 421
  • 1
  • 7
  • 22