I have encoded an object via bs-json and want to send it as a data via post using bs-axios.
33 │ let createTest = (p: Data.toBuyListItem) => inst->Instance.postData("/
test", p |> Data.encodeToBuyListItem);
...
This has type:
Js.Json.t (defined as Js.Json.t)
But somewhere wanted:
Js.t('a)
p |> Data.encodeToBuyListItem is red. How to use the Js.Json.t value as data for a post request?
Edit:
Well, this works:
let createTest = (p: Data.toBuyListItem) => inst->Instance.postData("/test", [%raw "p"]);
but I would prefer a non-hacky solution (preferably using bs-json, since I am using that for decoding JSON)...
There's no way to safely convert a
Js.Json.ttoJs.t, as the latter represents a JavaScript object, and JSON doesn't just represent objects. Butbs-axiosseems to throw safety out the window anyway, so you could just cast it unsafely:%identityis a general mechanism that can be used to cast between any two types, so this is as unsafe as it gets with regards to typing. You're basically saying "Look away while I swap these things around and trust me, I know what I'm doing". So make sure you do.Another alternative is to create a
Js.tobject directly. Reason has built-in syntax for that, so it's pretty easy:someDatahere can be any value, even aJs.Json.tvalue, but that also means you can pass in values that aren't serializable, whichJs.Json.tprotects you against.Given that the
bs-axiosAPI is inherently unsafe there's a minor safety trade-off here, but I think whichever is easiest will do fine.