Ark
Ark

Reputation: 1383

POST request with Json body to MVC resful service

Trying to construct valid request to wcf restful service. But whole hour cannot do it, i dont get it why it's not work. Other APis what returns JSON works correctly, i tested them. Now need to fix request API. enter image description here

I put break point to implementation of AddMeal method but she don't triggered. I think something wrong with my request or with Attributes

    //Add Meal
    [OperationContract]
    [WebInvoke(UriTemplate = "AddMeal",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json, Method = "POST")]
    bool AddMeal(string meal);

Upvotes: 0

Views: 669

Answers (1)

James
James

Reputation: 82136

Your WCF method is expecting a string, however, the model binder will try to bind your JSON to a string which won't work. When using MVC you should use models instead, create a model that represents your JSON data e.g.

public class Meal
{
    public DateTime EatedTime { get; set; }
    public decimal Amount { get; set; }
    public int PatientID { get; set; }
    public int MealTypeID { get; set; }
    ...
}

Then update your signature to expect that model

bool AddMeal(Meal meal)

You should find the data is bound to the meal model.

Upvotes: 1

Related Questions