Calling POST API in ASP.Net MVC with Payload Body Content

452 Views Asked by At

I have a 3rd party API as shown in below image. enter image description here

I'm trying to call this API in ASP.Net MVC controller as below:

public ActionResult GetQuickCatalogueAsync(string category = "f97b9ec8-5e74-4f20-8582-471d067fc6c4")
        {
            using (var client = new HttpClient())
            {
                var data = JsonConvert.SerializeObject(new
                {
                    Content = new { categoryId = category },
                    Key = "test",
                    Name = "get-products"
                });
                StringContent content = new StringContent(data, Encoding.UTF8, "application/json");
                
                var res = client.PostAsync(_baseUrl, content).Result.Content.ReadAsStringAsync().Result;
                var response = JsonConvert.DeserializeObject<Product>(res);

                return Content("");
            }
        }

The PostAsync is not getting Payload body (content) properly here.

Generated data object:

{"content":{"categoryId":"f97b9ec8-5e74-4f20-8582-471d067fc6c4"},"key":"test","name":"get-products"}

Expected:

{"Name":"get-products","Key":"test","Content":"{ \u0022categoryId\u0022:\u0022f97b9ec8-5e74-4f20-8582-471d067fc6c4\u0022}"}

And the Error on Post

{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"00-d1fb352d7eb19d4cbdb542bc7cee59fb-e62da6de34faee4b-00","errors":{"$.content":["The JSON value could not be converted to System.String. Path: $.content | LineNumber: 0 | BytePositionInLine: 12."]}}
0

There are 0 best solutions below