DNN Forums

Ask questions about your website to get help learning DNN and help resolve issues.

Making Json and Xml get along at a single endpoint

Sort:
You are not authorized to post a reply.





New Around Here





    Hi all --

    I have an API controller that receives data from several outside services (more property, several outside devices, I guess) via a POST request to a REST endpoint. When I started this project a looong time ago all of the devices were sending XML, so later when a few started needing Json it made more sense to hack in support for that and move on to the next thing. My controller routes POST requests to a parameterless method that looks at the Content-type header and calls an appropriate unpacker to return a data object, which is the same regardless of how the data arrives.

    The decisions all made sense at the time, and I was a much less skilled programmer at the time I started this. I probably wouldn't do it this way today.

    The current problem is this: 

    • Now the tables are turned, most of the devices send Json.
    • I need to make some other updates to this codebase and this wonky handling annoys me tremendously, and makes it impossible to switch to a newer framework that I've built over the years, and the unpacking method is slow (although that's largely an academic annoyance, it has no real-world impact).
    • It is difficult-bordering-on-impossible to change the URL on every source, or even every XML or JSON source to route to different endpoints, I need to keep a single address. 

    Over the years, I've built (as I'm sure most everyone has) a framework that I use for most of my projects. Web API's built-in unpacking of Json and x-www-form data route a data object to a generic POST method, and I have other methods that would live on other endpoints when XML or form-data are needed. This has worked really well over the years, Xml and form-data have become such outliers that it's not a problem -- except with this project.

    So here's my question -- From all the research that I've done there are two ways that Web API will unpack XML data (cleanly) and deliver a valid data object to Post(dataObject item), exactly the same as it does for Json and x-www-form right now:

    • Enabling the XmlSerializer in WebApiConfig.cs, which I presume I don't have any access to without creating a non-standard DNN instance. Is there any other way to enable this, either broadly or for my controller?
    • Using the default DataContractSerializer. This doesn't work because the XML doesn't pass a proper schema with it. The kicker is that WebApi appears to try to parse it, it reads the first 125(?) characters from the request stream before it fails to parse and calls Post(item: null). Since it's already started reading the stream (which is a network stream, no way to go back to the start, right??), it seems that I don't have any way of backtracking in the stream to feed it into a manual call to XmlSerializer. Is there any other way to do this?
    • New thought: Can I build a route constraint based on Content-type?

    This isn't my only reference on the matter, but summarizes my understanding pretty well: https://stackoverflow.com...ith-xml-data-on-post

    Anyone have any insight? It is entirely possible that I'm missing something huge here, I'm a solo coder and largely self-taugh on anything newer than Turbo Pascal and Turbo C++ for Windows 95, so sometimes all I need is someone to show me which boat I've missed...

    If the right answer is that I either need two endpoints at distinct urls or just keep doing it the hacky way I am doing it, I can probably just accept that and move on. I just have a hard time giving up the fight without knowing for certain there's no way to do it.

    Thanks in advance --
    -Tim

    You are not authorized to post a reply.

    These Forums are dedicated to the discussion of DNN Platform.

    For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

    1. If you have (suspected) security issues, please DO NOT post them in the forums but instead follow the official DNN security policy
    2. No Advertising. This includes the promotion of commercial and non-commercial products or services which are not directly related to DNN.
    3. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
    4. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
    5. No Flaming or Trolling.
    6. No Profanity, Racism, or Prejudice.
    7. Site Moderators have the final word on approving / removing a thread or post or comment.
    8. English language posting only, please.

    Would you like to help us?

    Awesome! Simply post in the forums using the link below and we'll get you started.

    Get Involved