.Net Framework Web API & Client - Empty JSON
-
I have this API controller method
[HttpGet]
public HttpResponseMessage GetPersonList(int personTypeId = 0, int clientRepId = 0)
{
var response = new HttpResponseMessage();try { var data = Repository.GetPersonList(personTypeId, clientRepId); response = Request.CreateResponse(HttpStatusCode.OK, data); // AT THIS POINT THE RESPONSE HAS DATA& } catch (Exception e) { response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); } return response;
}
It's working and has data, and when I call it from Postman I can see the complete JSON. Here's my client:
public class APIExecutor
{
private HttpClient _client;public APIExecutor(string baseAddress, HttpClient client = null)
{
if (client != null)
{
_client = client;
}
else
{
_client = new HttpClient();
}\_client.BaseAddress = new Uri(baseAddress); \_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public async Task<T> GetAsync<T>(string action, Dictionary<string, dynamic> parameters)
{
T results = default(T);try { var queryString = CreateQueryString(action, parameters); HttpResponseMessage response = \_client.GetAsync(queryString).Result; // FOR TESTING - RETURNS EMPTY JSON \[\] var xxx = await response.Content.ReadAsStringAsync(); //<=== EMPTY JSON if (response.IsSuccessStatusCode) { // Status code is 200 here var responseString = await response.Content.ReadAsStringAsync(); results = await ReadAsAsync<T>(response.Content); } else { HandleException(response); } } catch (Exception e) { throw; } return results;
}
private async Task<T> ReadAsAsync<T>(HttpContent content)
{
var results = JsonConvert.DeserializeObject<T>(await content.ReadAsStringAsync());return results;
}
}Anyone see what's wrong? Thanks
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
I have this API controller method
[HttpGet]
public HttpResponseMessage GetPersonList(int personTypeId = 0, int clientRepId = 0)
{
var response = new HttpResponseMessage();try { var data = Repository.GetPersonList(personTypeId, clientRepId); response = Request.CreateResponse(HttpStatusCode.OK, data); // AT THIS POINT THE RESPONSE HAS DATA& } catch (Exception e) { response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); } return response;
}
It's working and has data, and when I call it from Postman I can see the complete JSON. Here's my client:
public class APIExecutor
{
private HttpClient _client;public APIExecutor(string baseAddress, HttpClient client = null)
{
if (client != null)
{
_client = client;
}
else
{
_client = new HttpClient();
}\_client.BaseAddress = new Uri(baseAddress); \_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public async Task<T> GetAsync<T>(string action, Dictionary<string, dynamic> parameters)
{
T results = default(T);try { var queryString = CreateQueryString(action, parameters); HttpResponseMessage response = \_client.GetAsync(queryString).Result; // FOR TESTING - RETURNS EMPTY JSON \[\] var xxx = await response.Content.ReadAsStringAsync(); //<=== EMPTY JSON if (response.IsSuccessStatusCode) { // Status code is 200 here var responseString = await response.Content.ReadAsStringAsync(); results = await ReadAsAsync<T>(response.Content); } else { HandleException(response); } } catch (Exception e) { throw; } return results;
}
private async Task<T> ReadAsAsync<T>(HttpContent content)
{
var results = JsonConvert.DeserializeObject<T>(await content.ReadAsStringAsync());return results;
}
}Anyone see what's wrong? Thanks
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
Just a guess - why
dynamic
and does ReadAsStringAsync deal with "dynamic" content?Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP
-
Just a guess - why
dynamic
and does ReadAsStringAsync deal with "dynamic" content?Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP
The dictionary is parameters going to the controller and is working ok. The params make it to the controllwe. The problem is the JSON coming back is empty
-
I have this API controller method
[HttpGet]
public HttpResponseMessage GetPersonList(int personTypeId = 0, int clientRepId = 0)
{
var response = new HttpResponseMessage();try { var data = Repository.GetPersonList(personTypeId, clientRepId); response = Request.CreateResponse(HttpStatusCode.OK, data); // AT THIS POINT THE RESPONSE HAS DATA& } catch (Exception e) { response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); } return response;
}
It's working and has data, and when I call it from Postman I can see the complete JSON. Here's my client:
public class APIExecutor
{
private HttpClient _client;public APIExecutor(string baseAddress, HttpClient client = null)
{
if (client != null)
{
_client = client;
}
else
{
_client = new HttpClient();
}\_client.BaseAddress = new Uri(baseAddress); \_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public async Task<T> GetAsync<T>(string action, Dictionary<string, dynamic> parameters)
{
T results = default(T);try { var queryString = CreateQueryString(action, parameters); HttpResponseMessage response = \_client.GetAsync(queryString).Result; // FOR TESTING - RETURNS EMPTY JSON \[\] var xxx = await response.Content.ReadAsStringAsync(); //<=== EMPTY JSON if (response.IsSuccessStatusCode) { // Status code is 200 here var responseString = await response.Content.ReadAsStringAsync(); results = await ReadAsAsync<T>(response.Content); } else { HandleException(response); } } catch (Exception e) { throw; } return results;
}
private async Task<T> ReadAsAsync<T>(HttpContent content)
{
var results = JsonConvert.DeserializeObject<T>(await content.ReadAsStringAsync());return results;
}
}Anyone see what's wrong? Thanks
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
Kevin Marois wrote:
public async Task<T> GetAsync<T>(string action, Dictionary<string, dynamic> parameters)
{
...
HttpResponseMessage response = _client.GetAsync(queryString).Result;Why? You're in an
async
method, so don't use the potentially deadlock-inducing.Result
to synchronously get the result of a task. Justawait
the task.HttpResponseMessage response = await _client.GetAsync(queryString);
Also, your controller can be simplified to:
[HttpGet]
public IEnumerable<Person> GetPersonList(int personTypeId = 0, int clientRepId = 0)
{
return Repository.GetPersonList(personTypeId, clientRepId);
}If an unhandled exception is thrown, the framework will return an error response for you.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Kevin Marois wrote:
public async Task<T> GetAsync<T>(string action, Dictionary<string, dynamic> parameters)
{
...
HttpResponseMessage response = _client.GetAsync(queryString).Result;Why? You're in an
async
method, so don't use the potentially deadlock-inducing.Result
to synchronously get the result of a task. Justawait
the task.HttpResponseMessage response = await _client.GetAsync(queryString);
Also, your controller can be simplified to:
[HttpGet]
public IEnumerable<Person> GetPersonList(int personTypeId = 0, int clientRepId = 0)
{
return Repository.GetPersonList(personTypeId, clientRepId);
}If an unhandled exception is thrown, the framework will return an error response for you.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks. I made the changes you suggested. However, I'm still not seeing any data coming back. The deserialZed JSON is empty, whereas the controller IS returning data. Any thoughts on what's causing this?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
Thanks. I made the changes you suggested. However, I'm still not seeing any data coming back. The deserialZed JSON is empty, whereas the controller IS returning data. Any thoughts on what's causing this?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
A completely empty response, or an empty JSON array? If it's an empty JSON array, that would suggest your repository isn't returning any data. If it's a completely empty response, that would suggest a different problem. Either way, you'll probably need to compare the raw network request made by your code to the working one sent via Postman to see what's different.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
A completely empty response, or an empty JSON array? If it's an empty JSON array, that would suggest your repository isn't returning any data. If it's a completely empty response, that would suggest a different problem. Either way, you'll probably need to compare the raw network request made by your code to the working one sent via Postman to see what's different.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I solved the issue. Thank you for your help. I'd like to post my finished class to get everyone's thoughts. I'd like your opinion on it. I'll post it later today
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.