proudmonkey / AutoWrapper

A simple, yet customizable global exception handler and Http response wrapper for ASP.NET Core APIs.
MIT License
677 stars 82 forks source link

Response not proper with AWS Lambda #102

Open Vignesh-coder-coder opened 3 years ago

Vignesh-coder-coder commented 3 years ago

when publishing API in AWS Lambda and requesting then then response shows as two different object. like, { token: "test token" } { "statusCode": 200, "message": "GET Request successful.", "isError": false, "result": "" }

proudmonkey commented 3 years ago

Would you be able to show some code on how you used it?

rikameajay1 commented 3 years ago

Facing same issue on AWS Lambda @Vignesh-coder-coder were you able to resolve this?

eware4190 commented 2 years ago

@rikameajay1 or @Vignesh-coder-coder did you ever resolve this? I'm facing the same issue as well. but now I'm sure it's on the AWS Lambda side, not something with AutoWrapper.

to check, I created my own ResponseWrapper Middleware (see below image) and got the same result when it's hosted on AWS Lambda. works locally.

image

`using System; using System.IO; using System.Net; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Newtonsoft.Json;

namespace vendorApi.Infrastructure.Middlewares { public class ResponseWrapper { private readonly RequestDelegate _next;

    public ResponseWrapper(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        var currentBody = context.Response.Body;

        using (var memoryStream = new MemoryStream())
        {
            //set the current response to the memorystream.
            context.Response.Body = memoryStream;

            await _next(context);

            //reset the body 
            context.Response.Body = currentBody;
            memoryStream.Seek(0, SeekOrigin.Begin);

            var readToEnd = new StreamReader(memoryStream).ReadToEnd();
            var objResult = JsonConvert.DeserializeObject(readToEnd);
            var result = CommonApiResponse.Create((HttpStatusCode)context.Response.StatusCode, objResult, null);
            await context.Response.WriteAsync(JsonConvert.SerializeObject(result));
        }
    }

}

public static class ResponseWrapperExtensions
{
    public static IApplicationBuilder UseResponseWrapper(this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<ResponseWrapper>();
    }
}

public class CommonApiResponse
{
    public static CommonApiResponse Create(HttpStatusCode statusCode, object result = null, string errorMessage = null)
    {
        return new CommonApiResponse(statusCode, result, errorMessage);
    }

    public string Version => "1.2.3";

    public int StatusCode { get; set; }
    public string RequestId { get; }

    public string ErrorMessage { get; set; }

    public object Result { get; set; }

    protected CommonApiResponse(HttpStatusCode statusCode, object result = null, string errorMessage = null)
    {
        RequestId = Guid.NewGuid().ToString();
        StatusCode = (int)statusCode;
        Result = result;
        ErrorMessage = errorMessage;
    }
}

}`

AliOuanes commented 4 months ago

who solved this problem?