0

I have a calling application which have code like following (I can't change this app)

try
{
    string request = string.Format("UniqueId={0}&MobileNumber={1}&UssdText={2}&Type={3}&AccountId={4}", "1", "2", "3", "4",
        "5");


    using (HttpClient client = new HttpClient(new LoggingHandler(new HttpClientHandler())))
    {
        string url = "http://localhost/MocExternalEntityApis/MyUssd/Getdata3";
        client.DefaultRequestHeaders.ExpectContinue = false;

        StringContent content = new StringContent(request);
        content.Headers.Clear();
        content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

        var response = await client.PostAsync(url, content).ConfigureAwait(false);
        var readAsString = await response.Content.ReadAsStringAsync();

        client.Dispose();

    }
}
catch (Exception ex)
{
}

My web api controller where call is coming is always having empty object

    [HttpPost]
    [ActionName("GetData3")]
    public JsonResult<MyResponse> GetData3(MyInput obj)
    {
        if (obj != null)
        {
            Logger.DebugFormat("UniqueId:{0},  MobileNumber:{1},  UssdText:{2},  Type:{3},  AccountId:{4}",
                obj.UniqueId, obj.MobileNumber, obj.UssdText, obj.Type, obj.AccountId);
            if (obj.Type == "3")
            {
                Task.Factory.StartNew(async () =>
                {
                    await ProcessCallbackHandlingofPinRespone(obj.UniqueId, obj.MobileNumber,
                        obj.UssdText);
                });
            }
            else
            {
                Task.Factory.StartNew(async () =>
                {
                    await ProcessCallbackHandlingOfNotification(obj.UniqueId, obj.MobileNumber,
                        obj.UssdText);
                });
            }
        }
        else
        {
            Logger.DebugFormat("Empty Object");
        }
        return Json(new MyResponse { Status = "OK" });
    }


[Serializable]
public class MyInput
{
    [JsonProperty(PropertyName = "UniqueId")]
    public string UniqueId { get; set; }

    [JsonProperty(PropertyName = "MobileNumber")]
    public string MobileNumber { get; set; }
    [JsonProperty(PropertyName = "UssdText")]
    public string UssdText { get; set; }
    [JsonProperty(PropertyName = "Type")]
    public string Type { get; set; }
    [JsonProperty(PropertyName = "AccountId")]
    public string AccountId { get; set; }
}

What change i need to do in My web Api to consume the data.

Logs of the call to my api is like Request:

Method: POST, RequestUri: 'http://localhost/MocExternalEntityApis/MyUssd/Getdata3', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
  Content-Type: application/json
}
UniqueId=1&MobileNumber=2&UssdText=3&Type=4&AccountId=5
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Kamran Shahid
  • 3,954
  • 5
  • 48
  • 93
  • Just found a possible solution from https://stackoverflow.com/questions/40407884/how-can-i-read-json-from-a-stringcontent-object-in-an-apicontroller?rq=1 – Kamran Shahid Sep 19 '18 at 11:34

2 Answers2

0

Try adding the [FromBody] attribute to your controller action so it looks like so:

[ActionName("GetData3")]
public JsonResult<MyResponse> GetData3([FromBody]MyInput obj)
{
  ...
}

Simple types such as ints are bound automatically but for more complex types such as MyInput then the web api trys to read the value from the message body, using a media-type formatter. By providing the [FromBody] attribute then it'll force to read the request body as a simple type and should serialize it as you expect

More information can be found here: https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

MattjeS
  • 1,367
  • 18
  • 35
0

I were eventually able to get the submitted content via Request.Content

sample code as i mentioned in above comment is

public class ValuesController : ApiController {
    // POST api/values
    [HttpPost]
    public async Task Post() {
        var requestContent = Request.Content;
        var jsonContent = await requestContent.ReadAsStringAsync();

    }
}
Kamran Shahid
  • 3,954
  • 5
  • 48
  • 93