0

enter image description here pinata.js file

const FormData = require("form-data");

export const uploadJSONToIPFS = async (JSONBody) => {

  const url = `https://api.pinata.cloud/pinning/pinJSONToIPFS`;

  //making axios POST request to Pinata ⬇️

  return axios

    .post(url, JSONBody, {

      headers: {

        "Access-Control-Allow-Origin": "*",

        pinata_api_key: key,

        pinata_secret_api_key: secret,

      },

    })

    .then(function (response) {

      return {

        success: true,

        pinataURL:

          "https://gateway.pinata.cloud/ipfs/" + response.data.IpfsHash,

      };

    })

    .catch(function (error) {

      console.log(error);

      return {

        success: false,

        message: error.message,

      };
    });
};

export const uploadFileToIPFS = async (file) => {

  const url = `https://api.pinata.cloud/pinning/pinFileToIPFS`;

  //making axios POST request to Pinata ⬇️


  let data = new FormData();

  data.append("file", file);

  const metadata = JSON.stringify({

    name: "testname",
    keyvalues: {
      exampleKey: "exampleValue",
    },
  });
  data.append("pinataMetadata", metadata);

  //pinataOptions are optional

  const pinataOptions = JSON.stringify({

    cidVersion: 0,

    customPinPolicy: {

      regions: [

        {
          id: "FRA1",
          desiredReplicationCount: 1,
        },
        {
          id: "NYC1",
          desiredReplicationCount: 2,
        },
      ],
    },
  });
  data.append("pinataOptions", pinataOptions);


  return axios

    .post(url, data, {

      maxBodyLength: "Infinity",

      headers: {

        "Access-Control-Allow-Origin": "*",

        "Content-Type": `multipart/form-data; boundary=${data._boundary}`,
    enter code here
        pinata_api_key: key,
        pinata_secret_api_key: secret,
        Accept: "text/plain",
      },
    })
    .then(function (response) {
      console.log("image uploaded", response.data.IpfsHash);
      return {
        success: true,
        pinataURL:
          "https://gateway.pinata.cloud/ipfs/" + response.data.IpfsHash,
      };
    })
    .catch(function (error) {
      console.log(error);
      return {
        success: false,
        message: error.message,
      };
    });
};
[![enter image description here][1]][1]


  [1]: https://i.sstatic.net/fkDqe.jpg

tried all the solutions available on internet not able to solve.

1 Answer 1

0

Just try adding this extension to chrome CORS: Access-Control-Allow-Origin "https://chrome.google.com/webstore/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf?hl=en"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.