punn
punn2y ago

Getting raw body from request

Is there a way to get a raw unparsed body from a request object. In express it would be req.body I'm trying to pass this to the payload of my Stripe WH signature verification and it's not taking req.text().
10 Replies
ian
ian2y ago
does req.blob() or req.arrayBuffer() give you want you want?
punn
punnOP2y ago
arrayBuffer() doesn't work either and req.blob() is null
sshader
sshader2y ago
(also just to check -- we're awaiting req.text() ?)
punn
punnOP2y ago
tried several variations:
req.text()
req.arrayBuffer().toString()
Buffer.from(req.text())
Buffer.from(req.text()).toString() <- lol
req.text()
req.arrayBuffer().toString()
Buffer.from(req.text())
Buffer.from(req.text()).toString() <- lol
getting this error from stripe
punn
punnOP2y ago
No description
sshader
sshader2y ago
I'm looking at https://blog.cloudflare.com/announcing-stripe-support-in-workers/ since it has examples of using the fetch standard with stripe and it sure seems like what you're doing should work. Does printing req.text() look right (like a stringified JSON payload)?
ian
ian2y ago
what is typeof await req.text()?
punn
punnOP2y ago
yep its a stringified json payload string
ian
ian2y ago
The error message says it's a "parsed JavaScript object" - I remember there's some nuance to if you're doing both req.json() and req.text() - once you do one, it might consume the body. If you don't do anything but validate, and pass in await req.text() as the parameter (not just req.text()), does that help?
punn
punnOP2y ago
Ah actually the .text() works fine. It was just an issue on my end re: mismatched env variables. Stripe's error message was also slightly misleading. Thanks regardless!

Did you find this page helpful?