I have a .Net Core webapi that i have deployed on the AWS API Gateway. I created a middleware to process all incoming requests and outgoing responses. That middleware works perfectly fine on any local machine, which means, before http request hits my api controller, it goes through that middleware and being processed there correctly, but when it is deployed to AWS API Gateway, context.Request is always blank...
Here is how the middleware is implemented
public class RequestLoggingMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger _logger;
public RequestLoggingMiddleware(RequestDelegate next, ILoggerFactory loggerFactory)
{
_next = next;
_logger = loggerFactory.CreateLogger<RequestLoggingMiddleware>();
}
public async Task Invoke(HttpContext context, IRequestLogger requestLogger)
{
// this line prints In coming Raw Request ..... Microsoft.AspNetCore.Http.Internal.DefaultHttpRequest
_logger.LogInformation($"In coming Raw Request ..... {context.Request}");
// this line prints Request.ContentLength .....
_logger.LogInformation($"Request.ContentLength ..... {context.Request.ContentLength}");
// this line prints Request.Body ..... System.IO.MemoryStream
_logger.LogInformation($"Request.Body ..... {context.Request.Body}");
}
}
This is how it is configured in startup.cs Configure method
app.UseMiddleware<RequestLoggingMiddleware>();
Some more infomration regarding the issue.
-How this is configured on the API gateway? it is configured as Proxy+
"Events": {
"ProxyResource": {
"Type": "Api",
"Properties": {
"Path": "/{proxy+}",
"Method": "ANY"
}
},
"RootResource": {
"Type": "Api",
"Properties": {
"Path": "/",
"Method": "ANY"
}
}
}
Are you deploying the middleware as a lambda? No the middleware is a typical .NEt MVC middleware configured in startup.cs like
app.UseMiddleware();
- Are you performing any request mapping on the gateway?
No
- Is your eventual endpoint being invoked when you invoke the API gateway endpoint?
Yes and it does get the request object correctly... Its just that middleware where i am not getting the body part of the request. On my local computer or one one of my teammate's computer it works fine.
blank?context.Request.ContentLength == null? Is itGETorPOST?