0

I'm trying to build a model to receive data from a HTTPPOST.

The model is received and populated fine - except for IList<harsta> harequest

It shows as having a count of 1, but having null values against the fields: screenshot

My model is:

 public class HAR
 {
    public int api_version { get; set; }
    public IList<harsta> harequest { get; set; }
    public class harsta
    {
        public int ta_id { get; set; }
        public string partner_id { get; set; }
        public string partner_url { get; set; }
    }
   ...
   ...
    }

The Post data for harrequest is (should have 2 entries):

[{"ta_id":97497,"partner_id":"229547","partner_url":"http://partner.com/deeplink/to/229547"},
{"ta_id":97832,"partner_id":"id34234","partner_url":"http://partner.com/deeplink/to/id34234"}]

A screenshot from PostMan shows the form encoded data that is sent to the controller: ss2

Example Request (this is the example provided on the 3rd party website)

POST
http://partner-site.com/api_implementation/ha
BODY
api_version=4
&harequest=[{"ta_id":97497,"partner_id":"229547","partner_url":"http://partner.com/deeplink/to/229547"},{"ta_id":97832,"partner_id":"id34234","partner_url":"http://partner.com/deeplink/to/id34234"}]
&start_date=2013-07-01
...
&query_key=6167a22d1f87d2028bf60a8e5e27afa7_191_13602996000

I'm sure it's not mapping to my model, because of the way I've setup my model here:

    public IList<harsta> harequest { get; set; }
    public class harsta
    {
        public int ta_id { get; set; }
        public string partner_id { get; set; }
        public string partner_url { get; set; }
    }

Have I setup the model incorrectly, to receive the JSON data from the harequest field in the POST?

Mark
  • 7,778
  • 24
  • 89
  • 147
  • It is quite odd to see JSON in a form post. Maybe your should show us the relevant View code as well. – Sam Axe Nov 05 '13 at 10:24
  • Hi @Dan-o - I've not got as far as the View yet - I need to get the FORM POST into the model - then I can start working on the rest. The form data is coming from a 3rd party - so I have no control over it. I have to write code to conform to what they post. Thanks a lot, Mark – Mark Nov 05 '13 at 10:26
  • Please post the code where you are calling the httpost method `ha` - it's probably in the view (EDIT: OK, I didn't see previous comments - I see now you don't have control over it). – Tomaz Tekavec Nov 05 '13 at 10:29
  • The problem is you can't do a hybrid post like this and expect the automatic binding to work. It either has to be completely form-specific content or completely JSON. For example, in PostMan switch to `Raw` and set the content type to JSON and write pure JSON - it should work. – James Nov 05 '13 at 10:30
  • http://stackoverflow.com/questions/13196645/post-json-data-through-a-form-in-mvc4 - the manual decode suggestion looks promising. – Sam Axe Nov 05 '13 at 10:30
  • http://stackoverflow.com/questions/12894022/posting-json-to-apicontroller - this is also a great answer. – Sam Axe Nov 05 '13 at 10:31
  • Mark, what do you get if you change the parameter type from HAR to object? – Tomaz Tekavec Nov 05 '13 at 10:36
  • @James - that then gives the error: "No MediaTypeFormatter is available to read an object of type 'HAR' from content with media type 'text/plain'. – Mark Nov 05 '13 at 10:47
  • Tomaz - I'm sorry, I don't understand what you mean. – Mark Nov 05 '13 at 10:47
  • @MarkTaitlike I said you need to set the contet type to JSON (it's drop dow list next to the `Raw` button) - yours sounds like it's set to `Text` at the minute... – James Nov 05 '13 at 10:49
  • Mark, I meant if you can change the method as follows and try to parse the parameter "har" later: public Hotel_ta ha(object har) – Tomaz Tekavec Nov 05 '13 at 11:05
  • Thanks Tomaz - I've done that, but now har doesn't appear to have anything in it. – Mark Nov 05 '13 at 11:14
  • Hi - I think what I'll try and do (which is what I think you were eluding to @Dan-o) is change the model to accept a string for harequest - and parse it in some way in the controller to get the information from it. Thanks everyone for your effort in helping me. – Mark Nov 05 '13 at 11:21

1 Answers1

-1

First of all, I'm not exactly comfortable with the embedding of the Harsta class in the Har class. Not good practice separate them.

Secondly, I think your problem actually stems from the fact the property names in the JSON object(s) you are returning are enclosed in quotes. Get rid of the quotes for only the property names.

That is don't do this:

[{"ta_id":97497,"partner_id":"229547","partner_url":"http://partner.com/deeplink/to/229547"},
{"ta_id":97832,"partner_id":"id34234","partner_url":"http://partner.com/deeplink/to/id34234"}]

Do this instead:

[{ta_id:97497,partner_id:"229547",partner_url:"http://partner.com/deeplink/to/229547"},
{ta_id:97832,partner_id:"id34234",partner_url:"http://partner.com/deeplink/to/id34234"}].
Oswald Umeh
  • 121
  • 2
  • 9