Pinata-SDK: Invalid request format

Issue

I have an issue with the pinning of file/directory. When I use Postman, everything works fine, but with SDK I always receive an error Invalid request format

Code example:

const readableStreamForFile = fs.createReadStream('./data/1.dat');
pinata.pinFileToIPFS(readableStreamForFile)
    .then((result) => {
        //handle results here
        console.log(result);
    }).catch((err) => {
        //handle error here
        console.log(err);
    });

Version

@pinata/sdk@1.1.10

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 46 (8 by maintainers)

Most upvoted comments

Found it !

  async pinImage(dataURL) {
    const data = dataURL.replace(/^data:image\/png;base64,/, "");
    const buff = Buffer.from(data, "base64");
    const stream = Readable.from(buff);

    // ¡¡ THE HACK !!
    stream.path = "some_filename.png";

    const res = await pinata.pinFileToIPFS(stream);

    return res.IpfsHash;
  },

Basically the Pinata API complains if you don’t provide a filename for the file you want to pin. The hint was to look at the “Pin Manager” where file names are displayed for every upload.

@obo20 I believe the documentation / recipe book should point out this edge case or the API should return a better error message.

@eightyfive Man, I can’t thank you enough ❤️ I approve his solutions works the best

Confirming that the stream.path workaround works for me as well:

    const readable = Readable.from("your string here");
    readable.path = "some_name.png";

    const options = { ... };

    try {
        const uploadRes = await pinata.pinFileToIPFS(readable, options);
        console.log(uploadRes); <-- this is hit if you set readable.path
    } catch (err) {
        console.log(err); <-- this is hit if you do not.
    }

@eightyfive hack worked for me as well. But I would really really prefer uploading Buffer directly.

dude, you just saved my day, appreciate!! @eightyfive

The SDK should really add a function for uploading Buffers. Ran into this same problem.