serlingpa serlingpa - 4 years ago 127
JSON Question

Difficulty sending JSON to Web API 2

I am having a hard time pleasing Web API 2 and trying to get it to process my JSON in an HTTP POST correctly. I am expecting the value of

UploadRequest.RequestItems[0].ItemTypeId
to be 1.

My JSON looks like this:

claim = {
"UploadRequests": [
{
"RequestItems": [
{ itemTypes: [1] }
]
}
],
"ClaimRef": "C/123",
"PerilId": 1,
"Title": 0,
"CustomerName": "Jones",
"Mobile": "07123123123",
"Email": "my.email@gmail.com"
};


It is the
RequestItems
it is falling over on. It passes the
ModelState.IsValid
, but the
RequestItems
is not being populated as expected. A watch of the claim on the Web API endpoint looks like this:

enter image description here

I am expecting the
ItemTypeId
to correspond to the JSON.

My
UploadRequest
model looks like this:

namespace DocumentPortalShared.Models
{
public class UploadRequest
{
public int Id { get; set; }
public int ClaimId { get; set; }
public string UrlId { get; set; }
public string RequestText { get; set; }
public RequestStatus Status { get; set; }
public DateTime CreateDate { get; set; } = DateTime.Now;
public DateTime? ResponseDate { get; set; }

public Claim Claim { get; set; }

public List<RequestItem> RequestItems { get; set; }
public List<ResponseItem> ResponseItems { get; set; }
}
}


and the
BaseItem
, from which
RequestItem
is derived, looks like this:

namespace DocumentPortalShared.Models
{
public abstract class BaseItem
{
public int Id { get; set; }
public int UploadRequestId { get; set; }
public int ItemTypeId { get; set; }

public ItemType ItemType { get; set; }
}
}


How should I be formatting the JSON?

Answer Source

I didn't see you sepcify ItemTypeID in the JSON. Maybe this......

claim = {
    "UploadRequests": [
        {
            "RequestItems": [
                { "itemTypeId":1 }
            ]
        }
    ],
    "ClaimRef": "C/123",
    "PerilId": 1,
    "Title": 0,
    "CustomerName": "Jones",
    "Mobile": "07123123123",
    "Email": "my.email@gmail.com"
};

and if there were more than one...

    "RequestItems": [
        { "itemTypeId":1 },{ "itemTypeId":2 },{ "itemTypeId":3 }
    ]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download