'Ocelot Aggregator doesn't work for post method
Good afternoon community.
I present my case and then I ask the question.
In ocelot.json I have the corresponding configuration to consume the microservice method through post.
I execute the request according to the configured route and it responds correctly.
Then I define an aggregate, because I need to process the response before sending it back to the client but it throws me a 404. I think I have the route configured correctly, but I can't find the problem.
Can you help me?
ocelot.json
{
"Aggregates": [
{
"RouteKeys": [ "account" ],
"UpstreamPathTemplate": "/api/login",
"Aggregator": "LoginEmployeeAggregator"
}
],
"Routes": [
{
"DownstreamPathTemplate": "/api/employees/loginEmployee",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 44305
}
],
"UpstreamPathTemplate": "/api/account",
"UpstreamHttpMethod": [ "Post" ],
"Key": "account"
}
],
"GlobalConfiguration": {
"BaseUrl": "https://localhost:7014"
}
}
LoginEmployeeAggregator.cs
public class LoginEmployeeAggregator : IDefinedAggregator
{
public async Task<DownstreamResponse> Aggregate(List<HttpContext> responses)
{
if (responses.Any(r => r.Items.Errors().Count > 0))
{
return new DownstreamResponse(null, HttpStatusCode.BadRequest, (List<Header>)null, null);
}
var userResponseContent = await responses[0].Items.DownstreamResponse().Content.ReadAsStringAsync();
var users = JsonConvert.DeserializeObject<List<UserDTO>>(userResponseContent);
var postByUserString = JsonConvert.SerializeObject(users);
var stringContent = new StringContent(postByUserString)
{
Headers = { ContentType = new MediaTypeHeaderValue("application/json") }
};
return new DownstreamResponse(stringContent, HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "OK");
}
}
Startup.cs
//...
builder.Services.AddOcelot()//Registered 'AddOcelot' service.
.AddSingletonDefinedAggregator<LoginEmployeeAggregator>();
//...
when call post to https://localhost:7014/api/account -> ok(200) when call post to https://localhost:7014/api/login -> error(404)
Solution 1:[1]
Ocelot supports Http GET method only. See documentation: Aggregation only supports the GET HTTP Verb.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Michal Vlk |