0

I recently ran into an error, I guess caused by my lack of understanding of how HTTP works, where I was sending a JSON object to the server to be stored in session data, and then sent back to the client at a later time.

When I was sending the data to the server all of my booleans were enclosed in quotes and so when they were later sent back to the client and parsed, all of my false values were evaluated as true - obviously quite a nooby error but it took me ages to debug because I was being stupid and seeing the "false" values in chrome dev tools and thought everything was fine with the response from the server and was looking for the problem elsewhere.

So I was wondering, is there a way to set a content-type header when sending data to the server, that would cause data types to be maintained, as you can when sending a response from the server to the client?

  • 8
    Sending data across HTTP is always textual string. The problem is in your JSON encoding/decoding to those strings. This will depend on what kind of library are you using for this. So you should be more concrete in what libraries and frameworks are you using. – Euphoric Dec 19 '13 at 10:30
2

JSON is a string. Unless you had a server-side re-processing of the data, you should have gotten back exactly what you sent, plus or minus some line breaks. If your JSON parser on the client doesn't correctly interpret a serialized file back to a shallow-copy of the same object, you've got a bad JSON parser. (true, false, and null are valid according to the standard.)

If you need to explicitly state the type of a field, you should consider a format with that information in the string. XML with an XSD (or DTD) can achieve that, or you could just include a flag in your JSON denoting the value type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.