Fetch: body as record?

Hello there,
I use bs-fetch and I want to make a body from a Record.
Let’s say I want my body to be:

type bodyRecord = { data: string }

let bodyRecord = {
data: "helloworld"
}

As far as I understand, I can create a body from a string, with the following:

Fetch.RequestInit.make(~body=Fetch.BodyInit.make("{data: \"helloworld\"}"))

I can manually create a string, but I want to know if:

  • I can automate the process with a function like RecordAsString
  • Build my body from my record without “stringify” it.

Hi, I hope this helps. It is what I use.

1 Like

There isn’t a safe way to do this quickly, although there are unsafe ways. To do it properly the data needs to be decoded and encoded. There are a few ways to do that, as discussed in this thread:

The suggested libraries all (I believe) support both encoding and decoding records.

Side note: I’m planning to update the bs-fetch API after I merge it into rescript-webapi. If you have ideas on ways to make it easier to use please describe them in this ticket.

1 Like