1

I want to get JSON response from Web API Call. I am calling it like below

HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://myapi.proj.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization", "Basic TUNTRTpNQ1MhZTIwMTc=");
client.DefaultRequestHeaders.Add("ClientURL", "http://123.com");
var response = client.PostAsJsonAsync("api/name/", priceRequest).Result;

I am unable to JSON in response variable. I want to get JSON response and assign it to my class which is somewhat like below

public class Information
{
    public int id{ get; set; }
    public string Name{ get; set; }
    public string address{ get; set; }
}

2 Answers 2

2

Try this:

Information obj = await response.Content.ReadAsAsync<Information>();

You need to reference System.Net.Http.Formatting assembly where extension method ReadAsAsync<T> is defined.

Sign up to request clarification or add additional context in comments.

11 Comments

i am using VS 2013
How it relates to my answer? :)
Probably your method is not async. I also see that you call client.PostAsJsonAsync("api/name/", priceRequest).Result instead of await client.PostAsJsonAsync("api/name/", priceRequest). It's a bad practice. Check this: learn.microsoft.com/en-us/dotnet/csharp/async
which Library to add to use await?
It's a native C# construct. Check the article from above comment too learn basics of async programming in c#.
|
0

I found myself

This is what i used

var result = response.Content.ReadAsStringAsync().Result;
var test = Newtonsoft.Json.JsonConvert.DeserializeObject(result);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.