admin管理员组

文章数量:1122833

I am using the following code to make the request by native fetch.

  const blob = new Blob([content], {type: 'text/plain'});
  const file = new File([blob], buildFileName(fileName));
  const data = new FormData();
  data.append('file', file);
  data.append(
    'pinataMetadata',
    JSON.stringify({name: buildFileName(fileName)})
  );
  data.append('pinataOptions', JSON.stringify({cidVersion: 1}));

  const res = await fetch('/pinning/pinFileToIPFS', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${process.env.PUB_PINATA_JWT}`,
    },
    body: data as any,
  });

This errors out with:

Invalid request format.

I even tried setting content-type manually in the headers above:

'Content-Type': 'multipart/form-data'

But then, it fails with Boundary Not Found.

Then, I switched to using fetch from undici library and it worked right away even without setting the content type header.

Unfortunatelly, I don't want to use undici because I don't want to use many libraries. I am using node 20 but also tried this for node 18.

Why does it work with undici's fetch but not with native fetch ?

本文标签: javascriptNodejs Invalid request format when using fetchStack Overflow