Dominic Gozza
Dominic Gozza

Reputation: 81

How to get the contents of the response of an HttpClient call

  public WorkItem CreateWorkItem(string title, string description, string comments)
    {
        HttpClient client = new HttpClient();
        //var response = client.PostAsync("http://localhost:57765/API.svc/CreateWorkItem?title="+title+"&description="+description+"&history="+comments, null).Result.Content;
        //var task = await client.PostAsync("http://localhost:57765/API.svc/CreateWorkItem?title=" + title + "&description=" + description + "&history=" + comments, null);
        var response = client.PostAsync("http://localhost:57765/API.svc/CreateWorkItem?title=" +title+"&description="+description+"&history="+ comments, null).Result;
        var result = response.Content.ReadAsStringAsync().Result;
        var workItem = JsonConvert.DeserializeObject<WorkItem>(result);
        return workItem;
    }

enter image description here Issue is: reading the response of my call to the API,

I also get an error when using "Content" Task does not contain a definition for 'Content' and no extension method 'Content' accepting a first argument of type "Content" Task could be found

Goal is to use ajax to hit this method to fire off the api call and populate my WorkItem with data and to read that response after the call is completed.

 public class WorkItem
{
    public string title { get; set; }
    public string description { get; set; }
    public string status { get; set; }
    public string comments { get; set; }
}

Upvotes: 2

Views: 13789

Answers (2)

SharmaPattar
SharmaPattar

Reputation: 2664

client.PostAsync()

is an asynchronous method and it will return only the task not the result. If you want to get the result back then you need to await the response from the API. using

var response = await client.PostAsync("http://localhost:57765/API.svc/CreateWorkItem?title="+title+"&description="+description+"&history="+comments, null)
var workItem = JsonConvert.Deserialize<WorkItem>(response.Content);

This is how we can access the data. But to achieve this your method should be mark as 'async', then only you can make use the 'await' keyword. Another method is,

var response = client.PostAsync("http://localhost:57765/API.svc/CreateWorkItem").Result.Content
var workItem = JsonConvert.Deserialize<WorkItem>(response);

But this is not a good practice to use result on the awaitable method. Because we cant make use of the asynchronicity of that feature.

Otherwise,

var task = client.PostAsync("http://localhost:57765/API.svc/CreateWorkItem");
task.wait();
var workItem = JsonConvert.Deserialize<WorkItem>(task.Result.Content);

This also not a good approach. These are all the options you have if you are not using 'async' function.

public WorkItem CreateWorkItem(string title, string description, string comments)
{
    HttpClient client = new HttpClient();
    var task = client.PostAsync("http://localhost:57765/API.svc/CreateWorkItem?title="+title+"&description="+description+"&history="+comments, null);
task.Wait();
        var contents = task.Result.Content;
    }

Upvotes: 0

Ray Krungkaew
Ray Krungkaew

Reputation: 6965

You see that it's "PostAsync" meaning that it return Task<HttpWebResponse> so you have to await it or block it until you get result (.Result)

var response = client.PostAsync("http://localhost:57765/API.svc/CreateWorkItem?title="+title+"&description="+description+"&history="+comments, null).Result;

or

var response = await client.PostAsync("http://localhost:57765/API.svc/CreateWorkItem?title="+title+"&description="+description+"&history="+comments, null);

Then using NewstonSoft.Json

var workItem = JsonConvert.Deserialize<WorkItem>(contents);

Upvotes: 1

Related Questions