How to read incoming request data in loader or action (request.formData()
vs await request.json()
)
#4213
-
How do I read incoming request data in a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
A few times while developing Remix Apps I've mistakenly used This happened to me today, when I was using an 3rd party library in my Remix app, which was sending its POST data via a form submission ( Below is the error outputted when I attempted to use
Reading formDataTo read data that was submitted via form (where the request's const action = ({request}) => {
const formData = await request.formData() Reading JSON DataTo read JSON data that was submitted via a non-form (where the request's // web browser
fetch('url.com', {method: 'POST | DELETE | PUT', body:{..}} )
const action = ({request}) => { //typiically you might would encounter request.json() in an action that is resource route more often than not
const json = await request.json() |
Beta Was this translation helpful? Give feedback.
A few times while developing Remix Apps I've mistakenly used
request.json()
in my action in an attempt to grab the submitted data only to realize that I should be usingrequest.formData
because the incoming request was actually a form submission (you'll see'Content-Type' : 'application/x-www-form-urlencoded'
in the coming request).This happened to me today, when I was using an 3rd party library in my Remix app, which was sending its POST data via a form submission (
'Content-Type' : 'application/x-www-form-urlencoded'
) but I had mistakenly assumed it was coming in asapplication/json
.Below is the error outputted when I attempted to use
await request.json()
on the incomingrequest
, which…