PinataCloud / Pinata-SDK

Official SDK for the Pinata IPFS service
MIT License
273 stars 67 forks source link

I am getting the error "Invalid request format." when using the API.Please help #165

Closed jingledongding closed 5 months ago

jingledongding commented 9 months ago

It my code:`import {PINATA_JWT} from '@env'; import {useCallback} from 'react'; import ReactNativeBlobUtil from 'react-native-blob-util';

export function useStorageUpload() { const upload = useCallback( async (filePath: string): Promise<string | undefined> => { try { const file = await ReactNativeBlobUtil.fs.readFile(filePath, 'base64');

    const formData = new FormData();

    formData.append('file', file);

    const response = await fetch(
      'https://api.pinata.cloud/pinning/pinFileToIPFS',
      {
        headers: {
          Authorization: `Bearer ${PINATA_JWT}`,
          Accept: 'application/json',
          'Content-Type': 'multipart/form-data',
        },
        method: 'POST',
        body: formData,
      },
    );

    if (response.ok) {
      const data = await response.json();
      return `https://salmon-working-hedgehog-438.mypinata.cloud/ipfs/${data.IpfsHash}`;
    } else {
      throw new Error('Upload file to Ipfs failed');
    }
  } catch (err) {
    throw new Error('Upload file to ipfs failed');
  }
},
[],

); return {upload}; } request body:--e2ebff34-8822-4e3d-820e-ba28a493e274 content-disposition: form-data; name="file" Content-Length: 96916 ... ... ... MJvXcA/JhsoRfYWPRLOoslkYl/6Btgea7lglcF2BRysw== --e2ebff34-8822-4e3d-820e-ba28a493e274-- Response body:{"error":"Invalid request format."}`

platform:`System: OS: macOS 13.3.1 CPU: (10) arm64 Apple M1 Pro Memory: 72.78 MB / 16.00 GB Shell: version: "5.9" path: /bin/zsh Binaries: Node: version: 20.8.0 path: ~/.nvm/versions/node/v20.8.0/bin/node Yarn: version: 1.22.19 path: /opt/homebrew/bin/yarn npm: version: 9.9.0 path: ~/gm/GoodMorning/node_modules/.bin/npm Watchman: version: 2023.12.04.00 path: /opt/homebrew/bin/watchman Managers: CocoaPods: version: 1.14.3 path: /Users/jingxiaopeng/.rbenv/shims/pod SDKs: iOS SDK: Platforms:

jingledongding commented 9 months ago

Does this API support the content type "application/octet-stream"?

stevedylandev commented 5 months ago

Hey there! There is a certain format you have to use when uploading blobs via the API which we have documented here. Here is an example:

const JWT = 'YOUR_PINATA_JWT';

async function pinFileToIPFS() {
  try {
    const text = "Hello World!";
    const blob = new Blob([text], { type: "text/plain" });
    const data = new FormData();
    data.append("file", blob);

    const res = await fetch("https://api.pinata.cloud/pinning/pinFileToIPFS",
      method: "POST",
      headers: {
        Authorization: `Bearer ${JWT}`,
      },
      body: data,
    );
    const resData = await res.json();
    console.log(resData);
  } catch (error) {
    console.log(error);
  }
};

We are currently rewriting the SDK to better handles cases like this!