I am just learning how to work with an API. I have been able to request and get a security token and I am able to get authenticated to the API for a GET request to return the information I want. I use the code shown below to make my GET request.
client.BaseAddress = new Uri("https://api.example.com");
client.DefaultRequestHeaders.Accept.Clear();
//client.DefaultRequestHeaders.Accept.Add(new
MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization" ,"Bearer "+ accessToken);
client.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
HttpResponseMessage response =client.GetAsync"/api/v1/agencies/agencyId/[email protected]").Result;
if (response.IsSuccessStatusCode)
{
string json = "Need help here";
}
I get authenticated to the API but just can not figure out how to get the json data. I have found several examples that use a command prompt application with a local API; they all seem to use async with wait, but those do not seem to work in an ASP.Net web application.
Can anyone help me out or show an example that is using a web form?
You can get the json like that:
if (response.IsSuccessStatusCode)
{
string json = response.Content.ReadAsStringAsync().Result;
}
for async function, you can use that:
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
}