0

I'm building a REST api with python, tornadoweb and MongoDB. I'm looking for a simple way to handle client-server document exchanges with the lowest operations and the simplest API as possible. I'm facing a problem to serialize and deserialize MongoDB's ObjectId to JSON to send/receive data to and from the client.

I know there is two ways to fix this:

None of these solutions seems to be acceptable because:

  • MongoDB uses ObjectIds instead of strings for a reason (cf: MongoDb: Benefit of using ObjectID vs a string containing an Id?)
  • If the server is sending the id of a document with the syntax {"$oid": "..."}, when the client sends back this id without any modification, tornado's get_argument function is not able to handle properly the data it receives:

    > arguments: {'_id[$oid]': ['54f9c7ab834bac1b76846655'], ...}
    > POST [...] (127.0.0.1): Missing argument _id
    

Any idea about how to handle this issue gracefully? By gracefully I mean that I would like to handle the id in a way that is the most similar to what I would do if the id was a simple string.

Thanks a lot for your help. I did some researches, I'm surprised I couldn't find an answer to this question as both MondoDB and tornado are quite often used together. Maybe I'm missing a keyword to get the solution.

1
  • Another way to answer the question is: how to avoid jQuery's AJAX call to serialize an object {obj: {field: 'value'}} as {"obj[field]": "value"}
    – Romain G
    Mar 6, 2015 at 16:24

1 Answer 1

0

The object ID (and probably the entire POST body) should be encoded as JSON. There is no standard way to represent complex objects like this in a form-encoded body. jQuery uses the non-standard encoding obj[field]=value, but this is not universally supported and in particular Tornado does not support it. When using jQuery with Tornado you should use either JSON or "traditional" mode (although traditional mode wouldn't help here since it only works for lists, not mappings).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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