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.
name?name, however, this is just an example. The actual forms data and property names are different, and the names in the forms data do not conform to our coding standards (using Hungarian notation, and abbreviations for starters). If there are no better ways of doing it, I may have to bite the bullet and accept that these are the property names I will have to use, but prefer not to unless absolutely necessary.