James
James

Reputation: 1945

Get JSON response from Web API

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; }
}

Upvotes: 1

Views: 3131

Answers (2)

James
James

Reputation: 1945

I found myself

This is what i used

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

Upvotes: 0

CodeFuller
CodeFuller

Reputation: 31282

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.

Upvotes: 2

Related Questions