XML response from OData services

If you call OData services in AX 7 (Dynamics 365 for Finance and Operations), you typically get data in JSON format, like this:

{
  "@odata.context":"https://myaxinstance.cloudax.dynamics.com/data/$metadata","value":[
    {
      "name":"ElectronicPaymentTypes","kind":"EntitySet","url":"ElectronicPaymentTypes"
    },{
      "name":"ExpensePaymentDetails","kind":"EntitySet","url":"ExpensePaymentDetails"
    }
    ...
  ]
}

JSON is a simple, lightweight format with good support in many tools, but sometimes you would rather get XML. XML is by no mean deprecated – it’s more than a format; you get a whole platform with capabilities useful for validations (XML schema), querying (XPath, XQuery), transformations (XSLT) and so on. Or you simply have a component that accepts only XML and not JSON.

Fortunately OData services aren’t limited to JSON; they can return XML as well. Simply add HTTP header Accept with value application/atom+xml,application/atomsvc+xml,application/xml and you’ll start getting the same data in XML format:

<ODataServiceDocument xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.datacontract.org/2004/07/Microsoft.OData.Core">
  <EntitySets>
    <ODataEntitySetInfo>
      <Name>ElectronicPaymentTypes</Name>
      <Title i:nil="true" />
      <Url>ElectronicPaymentTypes</Url>
    </ODataEntitySetInfo>
    <ODataEntitySetInfo>
      <Name>ExpensePaymentDetails</Name>
      <Title i:nil="true" />
      <Url>ExpensePaymentDetails</Url>
    </ODataEntitySetInfo><ODataEntitySetInfo>
  ...
  </EntitySets>
</ODataServiceDocument>

If you use Postman, for example, this is where you can put the header:

Of course, that you get XML format doesn’t mean that you get the structure you want. You still may need to transform it to something more suitable for your purposes.

The problem is that seems to be respected only at the highest level, i.e. the list of available services. The individual services return JSON regardless of the header.

6 Comments

  1. Thank you for that, but what’s the point? If the response from Odat comes to JSON and we can not change it. I tried to call the OData service to specify this parameter in the header, but we get the response in the JSON format and I do not know how to change it to XML.
    I will be grateful for any information in this case

  2. Using Microsoft’s implementation of OData (Microsoft.AspNetCore.OData v7.4.1), this does not work. Has Microsoft dropped support for XML response format?

    • Dynamics 365 for Finance and Operations doesn’t use .NET Core (as far as I know), therefore you seem to be talking about a completely different scenario.
      I don’t know how much they reuse what they get from standard libraries and what’s logic specific to D365FO, but I wouldn’t automatically expect that D365FO behaves the same.
      Nevertheless I haven’t checked the behavior lately and it was always very limited anyway.

  3. No logro que imprima el Xml solo como valor en el Json, alguien sabe como hacerlo?

  4. hi Martin.

    Does this still work? I’ve tried it with postman against a 10.0.21 PU45 FinOps system and I can’t for love nor money get it to return XML. In Postman I’ve changed the Accept header, also tried various combinations of the application/atom+xml, application/atomsvc+xml and application/xml types. I’ve also tried to put them on content-type just in case.

    It would be good if you are able to confirm it’s no longer doing as it used to with a more recent version?

  5. I am also not able to get an XML back. Seems only to work with the $metadata service call.

Comments are closed.