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?

13
  • It is quite odd to see JSON in a form post. Maybe your should show us the relevant View code as well. Commented Nov 5, 2013 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 Commented Nov 5, 2013 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). Commented Nov 5, 2013 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. Commented Nov 5, 2013 at 10:30
  • stackoverflow.com/questions/13196645/… - the manual decode suggestion looks promising. Commented Nov 5, 2013 at 10:30

1 Answer 1

-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"}].
Sign up to request clarification or add additional context in comments.

3 Comments

Instead of just saying it's bad practice, explain why too.
Hi Oswald - I have no control over the posted data - so removing quotes etc isn't possible. I have to work with the data shown in my question. Thanks anyway, Mark
Also getting rid of the quotes make it invalid JSON, so don't do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.