0

I wanted to share with the community about an issue I had to overcome when working with a console app and communicating with a WebAPI service call.

Passing simple types as parameters is straight forward but passing a complex type wasn't as simple. I needed to serialize the type somehow and pass that as a parameter. My approach is as follows. I hope someone finds this useful

1 Answer 1

1

WebAPI method:

      public IHttpActionResult PurchaseOrders([FromUri]string parameters)
      {
      var criteria = new JavaScriptSerializer().Deserialize<PurchaseOrderManager.Criteria>(parameters);
      var result = PurchaseOrderManager.PurchaseOrderSummary(criteria);
      return Content(HttpStatusCode.OK, result);
      }

The client method calling the service...

 private static async Task<List<PurchaseOrderListModel>> GetPendingPurchaseOrdersByUser(string token, UserModel userModel)
{
    var service = ConfigurationManager.AppSettings["service:address"];

    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri(service);
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);

        var content = new StringContent(JsonConvert.SerializeObject(new
        {
            Filter = "PENDING",
            RequestType = "REQUEST"
        }), Encoding.UTF8, "application/json");
        var paramsValue = content.ReadAsStringAsync().Result;


        HttpResponseMessage response = await client.GetAsync($"purchaseorders/purchaseorders?parameters={paramsValue}");
        if (response.IsSuccessStatusCode)
        {

            var purchaseOrders = response.Content.ReadAsAsync<List<PurchaseOrderListModel>>().Result;
            //do work....
            //return some value
        }
    }
    return null;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Personally I would not do this. Why are you not just using PurchaseOrderManager.Criteria in your controller operation i.e. public IHttpActionResult PurchaseOrders([FromUri]PurchaseOrderManager.Criteria parameters)? Thanks for sharing but I would not recommend that anyone actually implement this approach.
Put simply, the issue being presented was not about the implementation details of the service, rather how one communicates from HttpClient API, I apologize for not making that clear
Why is it not possible to express your query criteria in either the resource URL or the query string? Just how complex is PurchaseOrderManager.Criteria? Is it possible to update the question with the source listing for PurchaseOrderManager.Criteria?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.