This is my code where I am uploading image in IPFS with Pinata API:

try {
            // Upload Image in Pinata
            const formData = new FormData();
            formData.append('file',image);
            
            const resFile = await axios({
                method: "post",
                url: "https://api.pinata.cloud/pinning/pinFileToIPFS",
                data:formData,
                maxContentLength: 'Infinity',
                headers: {
                    pinata_api_key: "",
                    pinata_secret_api_key: "",
                    "Content-Type": "multipart/form-data",
                }
            });

            console.log("This is Response data",resFile.data.IpfsHash);

            const metadata = JSON.stringify({
                "ipfsPinHash": resFile.data.IpfsHash,
                "name": name,
                "keyvalues": {
                  "description": description,
                  "price": price,
                }
            });
            console.log("This is Custom MetaData is here", metadata);

            const ImageMetaData = await axios({
                method: "post",
                url: "https://api.pinata.cloud/pinning/pinByHash",
                data: metadata,
                maxContentLength: 'Infinity',
                headers: {
                    pinata_api_key: "",
                    pinata_secret_api_key: "",
                    "Content-Type": "multipart/form-data",
                }
            });

            console.log("This is MetaData Response", ImageMetaData.data);
            const result = `https://ipfs.io/ipfs/${resFile.data.IpfsHash}`;
            // mintThenList(result)
        } catch (error) {
            console.log("ipfs uri upload error: ", error)
        }

This code uploading image successfully, but when I am uploading JSON data with image hash, it gives me this error:

Error -> "Incorrect request. Please send the correct format, { hashToPin: 'yourHash', pinataMetadata: (optional metadata)}"

Why I am getting this error? Please tell me, I am very confused.

1

There are 1 best solutions below

0
On

I believe the problem is that you are passing the request as "Content-Type": "multipart/form-data" when it should be "Content-Type": "application/json". That should do the trick!

Source