Posts

Showing posts from 2016

Web API (.NET): POSTing an object in XML format to an ApiController

Spanish version / Versión en Español Problem : In a .NET Web API app, inside a class which extends ApiController, it is desirable to receive an object in both XML and JSON format, in the most transparent form possible. Receiving the whole XML as a string, or using attributes (.NET's annotations) in the class to serialize are not acceptable solutions. What tends to happen is that the object is always set to null when calling the method. There are various possible solutions, among them adding an "=" at the beginning of the POST message body. But doing so is not optimal, because it's a .NET specific convention and would limit us (if a partner used an HTTP client made with C++ running on Linux, we'd be in trouble). We cannot receive a string directly because that would prevent us from receiving the object in JSON format. Solution : Receive the object with the attribute (.NET annotation) [FromBody], and enable the xml serialization inside our controller. A

Web API (.NET): POSTeando un objeto en formato XML a un ApiController

Versión en inglés / English version Problema : En un aplicación .NET de tipo Web Api, dentro de una clase que hereda de ApiController, se desea poder recibir un objeto tanto en formato XML como JSON de la forma más transparente posible. Recibir el XML como string, o poner atributos (las annotations de .NET) en la clase a serializar no son soluciones aceptables. Lo que suele ocurrir es que el objeto siempre llega como null. Hay varias soluciones posibles, entre ellas anteponer un "=" al principio del cuerpo del pedido POST. Pero eso no es aceptable, porque es algo específico de MVC y nos estaría limitando (si nuestro partner hace un cliente HTTP en C++ y Linux vamos a tener problemas). Tampoco podemos recibir un string porque eso nos impediría recibir JSON. Solución: Recibir el objeto con el atributo (en el sentido de annotation de .NET) [FromBody], y habilitar el serializador xml en nuestro controller. Un lugar ideal para hacerlo es en el constructor estático de