0

I am running into a strange problem and I have had a difficult time finding a solution. I am using Xamarin to build an app for both Android and iOS. The app does a call out to a Web Api service using a rest request. I have found that, in my Web Api service the object that is being passed to my Post method is null on iOS, but is completely intact for Android only after changing the Post method's parameter from my defined type to type of "Object".

I was able to identify a partial solution for after heeding the advice found in this stack overflow article: Web API complex parameter properties are all null. My object somewhat mirrors the example object in the article in that I have a decimal property, two date properties, and an array property. When I change my Post parameter to type "Object" then the object is successfully received by my Post method. When I test with using iOS though the parameter is still null. I have tried the following so far:

  • Write the object to debug output both before and after it was added to the http request. Everything looks correct before it is posted.
  • On the service side, included code inside of the Post method to view the parameter object's content by checking if it is null (this information was written to a log using log4net).

I have exhausted my knowledge and would appreciate any information and have not found any useful resources online.

UPDATE
Following is the code to call the service:

public async Task<ReportResult> SubmitReport(decimal amount, 
DateTime reportDate, byte[] image)
{
    var rc = new RestClient(new 
    Uri("https://service.example.com/Application"));

    var req = new RestRequest("api/Controller/", Method.POST);
    req.Serializer = new RestSharp.Portable.Serializers.JsonSerializer();

    req.AddHeader("token", Application.Current.Properties["token"] 
    as string);

    var report = new Report()
    {
        CustomerID = "Customer/" + 
        Application.Current.Properties["username"].ToString(),
        Amount = amount,
        ReportDate = reportDate,
        ReportImageExtension = "png",
        EntryDate = DateTime.Now,
        Image = image
    };

    req.AddBody(report);

    var results = await rc.Execute<ReportResult>(req);

    return results.Data;
}

And here is the signature of my Post method in the service:

public async Task<ReportResult> Post(Report report)

The first thing that I do in the Post method is check the report parameter. It exists in android but is null in iOS.

Here is the data that is receved by my Post method when the parameter's type is just "Object". This was sent from the Android OS:

{
  "CustomerID": "Customer/JohnDoe",
  "ReportDate": "2017-06-27T00:00:00-04:00",
  "EntryDate": "2017-06-27T11:01:17.382801-04:00",
  "Amount": 111.11,
  "Image": "/9j/4VFORXhpZgAASUkqAAgAAAAMA...y1P4bP/9k=",
  "ReportImageExtension": "png"
}

The Image property has been condensed by me since it is a large byte array.

DeveryDay
  • 161
  • 15

0 Answers0