如何在浏览器和 node.js 上使用其 HTTP API 将 Uint8Array 发布到 IPFS?

How do I publish an Uint8Array to IPFS, using its HTTP API on both browser and node.js?

我已经尝试了几个小时,但没有成功。相关的 API 端点是 block/put。 IT 要求 HTTP 请求使用 multipart/form-data,但我不确定该怎么做。这是一次尝试:

const req = require("xhr-request-promise");
const FormData = require("form-data");

(async () => {
  const form = new FormData();
  form.append("data", new Buffer([1, 2, 3]));
  console.log(await req("https://ipfs.infura.io:5001/api/v0/block/put", {
    method: "POST",
    body: form
  }));
})();

ipfs-http-client 非常适合这个任务:

const { create } = require('ipfs-http-client');

// connect to ipfs daemon API server
const ipfs = create('https://ipfs.infura.io:5001');

(async () => {
    await ipfs.block.put(new Uint8Array([1, 2, 3]));
})();