I'm following this tutorial and specifically using the code from the Basic Usage section.
When I send a GET request from Postman to http://localhost:9000/api/gateway, I get the right number of objects, as many as are in the database, but all attributes as null.
I tried changing the ReadAsStreamAsync to ReadAsStringAsync, and I actually got one big string, an array of all the records from the database.
The attributes were NOT null there, but I don't know how to parse a string into an IEnumerable of objects, so that doesn't help me much.
Anybody have an idea what I could be missing? See the controller method below.
Note that the method sends HTTP requests to localhost:5000, but the method itself receives incoming requests on localhost:9000.
The goal is to create an API Gateway, that calls another microservice and is not supposed to have a direct access to the database of the microservice it is calling.
public async Task<IActionResult> GetAllPatientsAsync()
{
var httpRequestMessage = new HttpRequestMessage(
HttpMethod.Get, "http://localhost:5000/api/patients")
{
Headers =
{
{ HeaderNames.Accept, "application/json" }
}
};
var httpClient = httpClientFactory.CreateClient();
var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage);
if (httpResponseMessage.IsSuccessStatusCode)
{
using var contentStream =
await httpResponseMessage.Content.ReadAsStreamAsync();
PatientDtos = await JsonSerializer.DeserializeAsync
<IEnumerable<PatientDto>>(contentStream);
}
if (PatientDtos == null) return NotFound(); // 404 Not Found
return Ok(PatientDtos);
}
Turns out it was a problem with JsonSerializer. The following changes did the trick for me:
PatientDtos = await JsonSerializer.DeserializeAsync<IEnumerable<PatientDto>>(
contentStream, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
More information here.