0

I have one API in my webservice App that receives an empty model after the post.

This is the code in my testclient that calls the API

private async void AddDriverPayment()
{
    ModelPalmDriverPaymentRequest modelPalmDriverPaymentRequest = new ModelPalmDriverPaymentRequest()
    {
        SCS_ID = int.Parse(gttDXTextEditAddDriverPaymentSCS_ID.Text),
        DriverID = int.Parse(gttDXTextEditAddDriverPaymentDriverID.Text),
        Amount = decimal.Parse(gttDXTextEditAddDriverPaymentAmount.Text),
        Remark = gttDXTextEditAddDriverPaymentRemark.Text,
        PaymentType = gttDXTextEditAddDriverPaymentPaymentType.Text,
        PaymentYear = int.Parse(gttDXTextEditAddDriverPaymentPaymentYear.Text),
        PaymentWeek = int.Parse(gttDXTextEditAddDriverPaymentPaymentWeek.Text),
        DocumentPath = gttDXTextEditAddDriverPaymentDocumentPath.Text,
        DatePayment = dateTimePickerAddDriverPayment.Value
    };

    string JsonData = JsonConvert.SerializeObject(modelPalmDriverPaymentRequest);
    System.Net.Http.StringContent restContent = new StringContent(JsonData, Encoding.UTF8, "application/json");
    HttpClient client = new HttpClient();
    try
    {
        var response = await client.PostAsync(comboBoxEditPalmAddDriverPayment.Text, restContent);

        if (response.IsSuccessStatusCode)
        {
            var stream = await response.Content.ReadAsStringAsync();
            ModelPalmDriverPaymentResponse Result = JsonConvert.DeserializeObject<ModelPalmDriverPaymentResponse>(stream);

            textBoxAddDriverPaymentResult.Text = Result.SCS_ID.ToString() + " " + Result.PaymentID.ToString();
        }
        else
        {
            textBoxAddDriverPaymentResult.Text = response.StatusCode + " " + response.ReasonPhrase;
        }
    }
    catch (Exception ex)
    {
        textBoxAddDriverPaymentResult.Text = ex.Message;
    }

}

And this is the controller code in the webservice

    [Route("palm/AddDriverPayment")]
    [ApiController]
    public class ControllerPalmDriverPayment : ControllerBase
    {
        private readonly RepositoryPalmDriverPayment _repositoryPalmDriverPayment = new();

        [HttpPost]
        public IActionResult AddDriverPayment(ModelPalmDriverPaymentRequest modelPalmDriverPaymentRequest)
        {
            try
            {
                return base.Ok(_repositoryPalmDriverPayment.AddDriverPaymemnt(modelPalmDriverPaymentRequest));
            }
            catch (System.Exception)
            {
                return base.BadRequest("Nope not working...");
            }
        }
    }

The model looks like this (I copied the model class from the service into the client, so I am sure they are exact the same)

    public class ModelPalmDriverPaymentRequest
    {
        public int SCS_ID;
        public int DriverID;
        public decimal Amount;
        public string? Remark;
        public string? PaymentType;
        public int PaymentYear;
        public int PaymentWeek;
        public string? DocumentPath;
        public DateTime DatePayment;
    }

When I try the code, I can see in debug of the testclient that when I post the data, the model is correct filled,

enter image description here

but then I can see in debug on the webservice that the received model is empty

enter image description here

I have other API's in this webservice that I test with the same client, they all do not have this problem.

I found this question but the answers don't help me

Anybody has any idea what the problem here is ?

EDIT

I found the problem, and wrote it in an answer so anybody with the same problem can find it.

GuidoG
  • 11,359
  • 6
  • 44
  • 79
  • have you tried to call your api with postman?or using [FromBody] attr on your method input? – AliSalehi Nov 05 '22 at 11:09
  • @AliSalehi How do I write [frombody] in my code? I don't know postman, how is that different? – GuidoG Nov 06 '22 at 12:08
  • like this : ```AddDriverPayment([FromBody]ModelPalmDriverPaymentRequest)``` – AliSalehi Nov 06 '22 at 19:13
  • Since you use json type data,you need to add [FromBody] to your action.If it still doesn;t work,try to check if the format of your [StringContent](https://stackoverflow.com/questions/6117101/posting-jsonobject-with-httpclient-from-web-api). – Yiyi You Nov 07 '22 at 07:31
  • @YiyiYou Yeah I would like to check what is in my `restContent` but how can I do that ? When in debug it shows my hundreds of properties but nothing where I can find the content – GuidoG Nov 07 '22 at 10:19
  • @AliSalehi I tried with [FromBody] but still the model is empty on the receiving side – GuidoG Nov 07 '22 at 10:22
  • @YiyiYou I tried with [FromBody] but still the model is empty on the receiving side – GuidoG Nov 07 '22 at 10:22
  • I have it working now, the problem was I forgot to provide getters and setters in the model – GuidoG Nov 07 '22 at 10:56

2 Answers2

0

Specify Content-Type: application/json on the client side or use the [FromBody] attribute on your ModelPalmDriverPayemntRequest parameter on the controller method.

More details about FromBody attribute here

fahirmdz
  • 7
  • 1
  • 4
  • I already have application/json on the client side, how should I write [frombody] in my code? – GuidoG Nov 06 '22 at 12:06
  • I also wonder why all my other api calls are working, I could not find a difference I copy pasted from a working api – GuidoG Nov 06 '22 at 12:12
  • I tried with [FromBody] but still the model is empty on the receiving side – GuidoG Nov 07 '22 at 10:21
  • I have it working now, the problem was I forgot to provide getters and setters in the model – GuidoG Nov 07 '22 at 10:56
0

I have it working now, the problem was I forgot to provide getters and setters in the model

So once I changed this

public class ModelPalmDriverPaymentRequest
{
    public int SCS_ID;
    public int DriverID;
    public decimal Amount;
    public string? Remark;
    public string? PaymentType;
    public int PaymentYear;
    public int PaymentWeek;
    public string? DocumentPath;
    public DateTime DatePayment;
}

into this

public class ModelPalmDriverPaymentRequest
{
    public int SCS_ID { get; set; }
    public int DriverID { get; set; }
    public decimal Amount { get; set; }
    public string Remark { get; set; }
    public string PaymentType { get; set; }
    public int PaymentYear { get; set; }
    public int PaymentWeek { get; set; }
    public string DocumentPath { get; set; }
    public DateTime DatePayment { get; set; }
}

It worked again

GuidoG
  • 11,359
  • 6
  • 44
  • 79