0

I have a rather unusual issue I've encountered.

We are currently developing an Ionic application - using Web API as back-end DB. Within the application, we call a $http.post method to post data back to the API, which looks like the following:

$http.post(
    ApiEndpoint.url + '/PostUpdate/',
    JSON.stringify(postData),
    {
        headers: {
            'Content-Type': 'application/json'
        }
    }
).success(function(data) {
    alert("WORKS!");
});

I can see all the data being POST-ed successfully via the Google Chrome Developer Tools:

enter image description here

However when the POST hits the Web API - not all of the data is featured in the model. Using the above data as an example, you can see that the element "DetailsOfConversation" has data in it - however it is set to null in the model:

enter image description here

however other properties (e.g. OutcomeId, TimeAttend, attendGivenAddress, jobId) have correct values set. This one has me stumped! Any ideas of where I can start hunting the problem down?

EDIT: I've implemented the following custom attribute: http://weblog.west-wind.com/posts/2013/Dec/13/Accepting-Raw-Request-Body-Content-with-ASPNET-Web-API

to allow me to view the raw body of the POST data, it seems the missing fields ARE there:

enter image description here

so i guess if this can't be solved, I'll simply extract the data from that (not ideal, I know)

Andrew Milici
  • 145
  • 2
  • 10
  • What's data type of DetailsOfConversation?! If it's not a string then you are passing a string so that property won't be serialized. – Bettimms Mar 22 '16 at 05:54
  • Hey there it's definitely a string – Andrew Milici Mar 22 '16 at 05:55
  • 1
    Try it without the JSON.stringify and pass the postData directly just to see if that works. You shouldn't have to stringify the data. – Lex Mar 22 '16 at 06:08
  • Thanks @Lex - tried that without luck - got exactly the same result :( – Andrew Milici Mar 22 '16 at 06:15
  • This should be working and it bugged me enough that I built a little app and ran it locally to test it. Everything worked fine, the data was POSTed to my WebApi controller method just fine - all the data was present. I tried both with and without JSON.stringify() and that didn't make a difference. I should note that I did not specifically set the content type, but when I inspected it it shows as `application/json;charset=utf-8`. – Lex Mar 22 '16 at 20:19
  • Thanks @Lex - appreciate your efforts - it SHOULD work - I agree! I've had similar weird issues previously with web API - eventually I've worked them out! This one has me stumped – Andrew Milici Mar 22 '16 at 21:51

2 Answers2

0

Please check your serializer and make sure that you have received content type 'application\json', probably you dont have registered json serialiser or content type accepted as'x-www-form-urlencoded'`. I had similar problem before and its source were in serializer.

  • I've set the JSON serialiser using the following in my Global.asax.cs: HttpConfiguration config = GlobalConfiguration.Configuration; config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented; take from: http://stackoverflow.com/questions/13274625/how-to-set-custom-jsonserializersettings-for-json-net-in-mvc-4-web-api and I'm also setting: 'Content-Type': 'application/json' in my $http.post – Andrew Milici Mar 22 '16 at 10:11
0

I've got a deadline to meet - so I've had to put a work-around in:

dynamic json = Newtonsoft.Json.JsonConvert.DeserializeObject(input);
string conversation = json.DetailsOfConversation;

The above gives me the data I need, not ideal but I guess a workaround.

Andrew Milici
  • 145
  • 2
  • 10