1

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.

9
  • i even tried an implementation from here... Same problem... in middleware the context.Request is always empty when the code is running on AWS API Gateway, on local machine it works fine.. salslab.com/a/… Commented Dec 5, 2019 at 23:56
  • What do you mean exactly by blank? Commented Dec 6, 2019 at 22:06
  • that means context.Request.ContentLength is empty .... Commented Dec 6, 2019 at 22:16
  • What request you are sending to your API when you have context.Request.ContentLength == null? Is it GET or POST? Commented Dec 6, 2019 at 22:26
  • The request type is "POST" Commented Dec 6, 2019 at 22:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.