I am writing a Web Api service to receive data from call back from a client's API. Since they specify their incoming services as receiving JSON, I have assumed that their callbacks would also post the data in JSON format. However, when it comes to testing my API, I have discovered that they are actually posting form data.
This isn't a problem in itself, as the API accepts POST data in several formats. However, when accepting the data as JSON, I can do something like this:
public IHttpActionResult Post(Message message)
{
...
}
public class Message
{
[JsonProperty("name")]
public string NameParameter { get; set; }
}
This allow me to post json such as { "name": "Dave" }
and refer to the parameter in my handler as message.NameParameter
.
However, when the data is received as forms data (application/x-www-form-urlencoded
), for example name=Dave
, the JsonProperty
attributes do nothing (hardly surprising given the attribute name), and message.NameParameter
ends up as null
.
Of course, I could just change the property name in the Message
class to match the POST parameter name, but in certain cases, the names of the POST parameters do not conform to our coding standards, so I would prefer a way to specify the binding via an attribute, in the same way JSON data is handled.