0

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.

Ed B
  • 785
  • 4
  • 9
  • How are you submitting the form? If this is an Ajax request, could you not format your data as JSON before you send it to the server? – Floremin Jun 01 '15 at 15:13
  • Why not just rename the model property to `name`? – DavidG Jun 01 '15 at 15:15
  • I'm not actually submitting the form data, I am writing the web service to receive the form data from a third party. I was originally expecting to get JSON posted to it (hence the use of JsonProperty), but didn't discover that it is actually old-school forms data until testing. – Ed B Jun 01 '15 at 15:16
  • In this example, it would be acceptable to change it to `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. – Ed B Jun 01 '15 at 15:18
  • 2
    This looks like the solution to your problem: http://stackoverflow.com/a/4316327/894792 – Luke Jun 01 '15 at 15:23
  • Thanks, that does look like the answer to my problem. It seems I couldn't quite find the right combination of search terms to find that answer! – Ed B Jun 01 '15 at 15:28
  • Having spent some time implementing the linked solution, I have still not been able to get this to work. It seems that there is some combination of MVC4, Web Api 2.0 and posting web forms data that means the custom model binder doesn't get used, even though I can see it getting instantiated. For now, I'm just going to have to bite the bullet and give ugly property names to my model class's properties... – Ed B Jun 02 '15 at 16:24

0 Answers0