HttpContext.Request is blank for a middleware running on AWS API Gateway

試著忘記壹切 提交于 2020-01-05 04:18:10

问题


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.

来源:https://stackoverflow.com/questions/59167427/httpcontext-request-is-blank-for-a-middleware-running-on-aws-api-gateway

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!