You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将对象(而非文件)固定到IPFS节点?API调用400错误排查

解决IPFS Kubo dag/put 400错误问题

错误原因

你的请求存在两个核心问题:

  1. Content-Type 错误:使用了application/json,但Kubo的dag/put接口需要IPLD专属的MIME类型(比如application/vnd.ipld.dag-json或application/octet-stream)。
  2. 请求体结构无效:IPLD节点的Data字段要求是base64编码的原始数据,而非直接传入CID;且你添加的"object data"字段是无效参数,Kubo无法识别。

正确实现方式

方式1:直接固定原始数据(字符串/二进制)

如果只是要把普通数据直接固定到节点,无需构建DAG结构,用application/octet-stream作为Content-Type,直接将数据作为请求体:

// 示例:固定字符串数据
const rawData = "需要固定的内容";
const res = await fetch(`${ipfsUrl}/dag/put?pin=true`, {
  method: 'POST',
  headers: { 'Content-Type': 'application/octet-stream' },
  body: rawData
});

if (res.ok) {
  const cid = await res.text();
  console.log("成功固定的CID:", cid);
} else {
  console.error("请求失败:", res.statusText);
}

方式2:构建IPLD DAG-JSON节点

如果需要带链接的DAG结构,使用application/vnd.ipld.dag-json类型,Data字段需传入base64编码的数据:

// 示例:构建带Data的DAG节点
const rawData = "需要固定的内容";
const base64Data = btoa(rawData); // 将原始数据转base64

const res = await fetch(`${ipfsUrl}/dag/put?pin=true`, {
  method: 'POST',
  headers: { 'Content-Type': 'application/vnd.ipld.dag-json' },
  body: JSON.stringify({
    Data: base64Data,
    Links: [] // 无链接则留空数组,有链接时添加{ Name: "链接名", Cid: { "/": "目标CID" } }
  })
});

if (res.ok) {
  const cid = await res.text();
  console.log("成功固定的CID:", cid);
} else {
  console.error("请求失败:", res.statusText);
}

内容的提问来源于stack exchange,提问作者noahacla

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 13:27:40