The Sheek Geek
The Sheek Geek

Reputation: 4216

HttpResponseMessage from HttpClient to User Defined Object C#

I have seen similar questions asked but none that seem to help me with my issue so please bear with me.

I have a WebAPI controller method that is defined as such:

[HttpPost]
[Route("")]
public HttpResponseMessage CreateMyObject(MyObjectRequest myObject)
{
    MyObject o;
    try
    {
        o = _serviceFactory.GetInstance().CreateMyObject(myObject);
    }
    catch (Exception ex)
    {
        ex.WriteToLog();
        throw ApiHelper.CreateResponseException(HttpStatusCode.InternalServerError, ex);
    }
    var response = Request.CreateResponse(HttpStatusCode.Created, o);
    var uri = Url.Link("GetMyObjectById", new { myObjectId = o.MyObjectId.ToString() });
    response.Headers.Location = new Uri(uri);
    return response;
}

Say, MyObject contains two properties,

public MyObject
{
    public Guid MyObjectId;
    public string MyObjectName
}

A client was written to call these controller methods in a WPF application. Here is the client method that is being used:

public HttpResponseMessage CreateQuote(MyObjectRequest myObject)
{
    var hashtable = new Hashtable
                {
                    {"myObject", myObject}
                };

    var task = GetResponse("", hashtable);
    var response = task.Result;

    return response;
} 

protected async Task<HttpResponseMessage> GetResponse(string path, Hashtable parameters)
{
    var response = await GetAsync(BuildRequestUri(path, parameters)).ConfigureAwait(false);
    return response.IsSuccessStatusCode ? response : new HttpResponseMessage();
}

protected async Task<HttpResponseMessage> GetResponse(string path)
{
    return await GetResponse(path, null);
}

The controller and supporting client code was not written by me and was already in the system. I am just consuming this in the WPF application. Now, I am trying to call the controller method via the client in the application and get the MyObject from the response so that I can access the MyObjectId that has been created and set. I have tried some of the other responses to similar questions but have not even seen some of the methods that are called on the response in order to get the information. Here is the first part of the call to the client that I have in the application:

var httpResponse = ApplicationService.CreateMyObject(myObjectRequest);

The application service simply injects the client into the constructor and allows me to call the CreateMyObject method. Is there any insight that can be given to me on how I should be getting the MyObject object out of the response?

Upvotes: 2

Views: 3357

Answers (1)

Andrew Dunaway
Andrew Dunaway

Reputation: 1226

I'm still a little new to web api as well, but I'm currently working with it on a project. Give the following code a try:

MyObject myObject;
if (response.IsSuccessStatusCode)
{
    // Parse the response body. Blocking!
    myObject = response.Content.ReadAsAsync<MyObject>().Result;
}

So you could theoretically change your method like this (may not be exactly what you want):

public MyObject CreateQuote(MyObjectRequest myObject)
{
    var hashtable = new Hashtable
                {
                    {"myObject", myObject}
                };

    var task = GetResponse("", hashtable);
    var response = task.Result;

    MyObject newObject;
    if (response.IsSuccessStatusCode)
    {
        // Parse the response body. Blocking!
        newObject= response.Content.ReadAsAsync<MyObject>().Result;
    }

    return newObject; // instead of response
} 

Upvotes: 1

Related Questions