2

I am hoping that someone can help me. I have a WCF REST service which is working correctly for ALL GET functionality. I am not able however to get the POST methods to work. My Service contract is

    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "NewEmployeeLeave/")]
    string addTimeOff(mmpTimeOff data);

which is calling the following

// addTimeOff
    // Add a new time off record with passed information
    public string addTimeOff(mmpTimeOff data)
    {
        try
        {
            // Add the new record to the employees leave requests
            mmpTimeOff newTimeOff = data;
            db.TimeOffs.Add(new TimeOff
            {
                StartDate = newTimeOff.StartDate,
                EndDate = newTimeOff.EndDate,
                PersonId = newTimeOff.PersonId,
                Manager = newTimeOff.Manager,
                TimeOffType = newTimeOff.TimeOffType,
                Status = newTimeOff.Status,
                Notes = newTimeOff.Notes
            });

            db.SaveChanges();
        }
        // Make sure that we can push the error back to the client.
        catch (Exception e)
        {
            return e.Message;
        }

        return "ok";
    }

The client code is as follows.

client = new HttpClient();
        HttpResponseMessage response=new HttpResponseMessage();
        mmpPerson user = new mmpPerson();
        client.BaseAddress = new Uri(url);
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        try
        {
            mmpTimeOff newLeave = new mmpTimeOff()
            {
                PersonId = user.Id,
                StartDate = DateTime.Parse("2017-10-22"),
                EndDate = DateTime.Parse("2017-10-28"),
                TimeOffType = "Annual",
                Manager = user.Manager,
                Status = "Appr"

            };

            response = await client.PostAsJsonAsync(url + "/newemployeeleave", newLeave);
            if (response.IsSuccessStatusCode)
            {
                ViewBag.Added = "Added New Leave Successfully";
            }

When the client is run i get a null value being sent through to the addTimeOff method.

The class is defined in the service as

public class mmpTimeOff
{
    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public System.DateTime StartDate { get; set; }
    [DataMember]
    public System.DateTime EndDate { get; set; }
    [DataMember]
    public string TimeOffType { get; set; }
    [DataMember]
    public int PersonId { get; set; }
    [DataMember]
    public string Status { get; set; }
    [DataMember]
    public Nullable<int> Manager { get; set; }
    [DataMember]
    public string Notes { get; set; }

}

and is bound to the client as a connected service.

How do i successfully get the object data through to the service.

All help gratefully received.

Ric

  • 1
    Because you are using `BodyStyle = WebMessageBodyStyle.Wrapped` you may find this answer helpful, it is explaining how to wrap your parameter https://stackoverflow.com/a/12572821/920557. Hope it helps. – Eugene Komisarenko Aug 01 '17 at 19:22

1 Answers1

0

Eureka!!!!

Thanks Eugene. After reading your response and checking out the link, I did some further investigation using POSTMAN and I discovered that PostAsJsonAsync is indeed doing the json conversion of the data being passed but as the StartDate and EndDate properties of the object are DateTime objects it seems that the json conversion does not like it.

I have changed the model to pass date strings instead and it is now working as expected.

Ric