mvbl fst
mvbl fst

Reputation: 5263

Node Express parses request body with JSON incorrectly

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:....

Any ideas why would this happen?

Upvotes: 0

Views: 115

Answers (1)

Peter Lyons
Peter Lyons

Reputation: 146074

I suspect your browser isn't actually sending JSON, it's sending application/x-www-form-urlencoded. This is not the correct value if you are trying to have the browser send JSON: myvalue[AudioEncoder][Settings][1][audio_bitrate]:16000. That's not JSON. Check the request headers for Content-Type and look at the raw body of the request to verify this. If you post your browser JS that's sending the AJAX, we can help you fix that. jQuery makes it a little tricky to specify the options correctly to get it to really send JSON.

Upvotes: 1

Related Questions