How to pin an object (as opposed to a file) to and IPFS node?

43 Views Asked by At

I am trying to figure out how to pin data to an IPFS node without putting said data into a file. I've read through the kubo docs and am unclear specifically where it refers to the object data argument.

const res = await fetch(`${ipfsUrl}/dag/put?pin=true`, {
        method: 'POST',
        headers: { 'Content-Type':'application/json' },
        body: JSON.stringify({ Data: cid, "object data": "multipart/form-data" })
      })

This is the request I am currently trying (have tried several variations).

Response {
  [Symbol(realm)]: null,
  [Symbol(state)]: {
    aborted: false,
    rangeRequested: false,
    timingAllowPassed: true,
    requestIncludesCredentials: true,
    type: 'default',
    status: 400,
    timingInfo: {
      startTime: 3301.874954223633,
      redirectStartTime: 0,
      redirectEndTime: 0,
      postRedirectStartTime: 3301.874954223633,
      finalServiceWorkerStartTime: 0,
      finalNetworkResponseStartTime: 0,
      finalNetworkRequestStartTime: 0,
      endTime: 0,
      encodedBodySize: 40,
      decodedBodySize: 40,
      finalConnectionTimingInfo: null
    },
    cacheState: '',
    statusText: 'Bad Request',
    headersList: HeadersList {
      cookies: null,
      [Symbol(headers map)]: [Map],
      [Symbol(headers map sorted)]: null
    },
    urlList: [ URL {} ],
    body: { stream: undefined }
  },
  [Symbol(headers)]: HeadersList {
    cookies: null,
    [Symbol(headers map)]: Map(5) {
      'content-type' => [Object],
      'vary' => [Object],
      'x-content-type-options' => [Object],
      'date' => [Object],
      'content-length' => [Object]
    },
    [Symbol(headers map sorted)]: null
  }
}

this is the response Im getting

1

There are 1 best solutions below

2
On

I believe the kubo RPC API requires a file vs JSON but you can pretty easily turn that data into a file with a few extra lines of code:

const data = JSON.stringify({ Data: cid })
const blob = new Blob([data], { type: 'application/json'})
const file = new File([blob], "data.json", { type: 'application/json'})
const formData = new FormData()
formData.append("files", file)

const res = await fetch(`${ipfsUrl}/dag/put?pin=true`, {
        method: 'POST',
        headers: { 'Content-Type':'multipart/form-data' },
        body: formData
      })

Depending on your use case writing to file might be handy for backups not on IPFS, which is similar to how we handle our pinJSONToIPFS endpoint