Home > Software design >  Cannot de-serialize an object passed from client console app to WebApi endpoint
Cannot de-serialize an object passed from client console app to WebApi endpoint

Time:06-24

In Visual Studio 2022, testing a client console project communicating with a web api project.

The client can call into the web api endpoint, but I cannot get the data to deserialize on the web api side.

This Product class currently lives in the WebApi project

public class Product
{
    public string ProductName = string.Empty;
    public int ProductId = 0;
    public List<string> ProductInfo = new List<string>();
}

In my client console project, I have this method:

private Product BuildProductObject()
{
    Product p = new Product();
    p.ProductName = "TheBestProduct";
    p.ProductId = 1234;
    return p;
}

to use in building a Product object that is serialized and used in the PutAsJsonAsync call:

 ...
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://localhost:44138/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

Product p = BuildProductObject();
HttpResponseMessage response = await client.PutAsJsonAsync($"api/Product", JsonConvert.SerializeObject(p));
 ...

In my WebApi project, I have my Product controller with the following end point and string parameter. It is reached from the code above.

[HttpPut]
public async Task<IActionResult> Put([FromBody]string product )
{
    Product dProduct;
    ...
    try
    {
        dProduct = JsonSerializer.Deserialize<Product>(json: product);
    }
    catch (Exception ex)
    {
        string message = ex.Message;
    }
    ...
}

However dProuct does not contain the data from product after the Deserialize call. I confirmed that product has the data. No error is thrown, but dProduct does not have the data from string product after the deserialize call.

In trying a different approach, in the client I changed the PutAsJsonAsync call to have the product object directly:

HttpResponseMessage response = await client.PutAsJsonAsync($"api/Product", p);

And likewise I changed the signature in the web api method:

[HttpPut]
public async Task<IActionResult> Put([FromBody]Product product )

Again the endpoint is reached, but when I examine product, again it has no data. It's members are not populated.

I'd like to get either approach working, can anyone assist with what I am not doing properly? Thanks BB.

CodePudding user response:

since you are using PutAsJsonAsync not just PutAsync, you don' t have to serialize data, it serializes automatically

Product p = BuildProductObject();
HttpResponseMessage response = await client.PutAsJsonAsync($"api/Product",p);

and you have to add getters setters to your class, since you are using it as an action input parameter

public class Product
{
    public string ProductName { get; set; }
    public int ProductId { get; set; }
    public List<string> ProductInfo { get; set; }
}

public async Task<IActionResult> Put([FromBody]Product product )
  • Related