express icon indicating copy to clipboard operation
express copied to clipboard

Express parses request body with JSON incorrectly

Open shubik opened this issue 13 years ago • 2 comments

I am sending a JSON object from web client, which looks like this:

{"AudioEncoder":{"Settings":{"1":{"audio_bitrate":"16000"}}}}

And in the request I get from req.body.myvalue:

{"AudioEncoder":{"Settings":[null,{"audio_bitrate":"16000"}]}}

In the Network panel of my browser I see correct value though:

myvalue[AudioEncoder][Settings][1][audio_bitrate]:16000

Error is where I am expecting object with key {1:... but get [null:...

shubik avatar Jul 31 '13 17:07 shubik

By the information you provide it looks like you're converting your object to url-encoded form data (application/x-www-form-urlencoded), not sending as JSON (application/json). I'd recommend sending your data as JSON instead.

Even if you wanted to send your data as application/x-www-form-urlencoded, I wouldn't consider this a bug since that representation doesn't have any real way to distinguish between objects and arrays.

gmethvin avatar Jul 31 '13 17:07 gmethvin

yea application/x-www-form-urlencoded is pretty sketchy since there's no spec, definitely recommend json as well for more complex stuff like this

tj avatar Aug 01 '13 16:08 tj