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

APIResponse produces invalid JSON. #47

Closed nasserkhalifa closed 4 years ago

nasserkhalifa commented 4 years ago

Hey,

I've installed this package to a .net core web API project and tried to call the APIs from an angular app there was a problem with parsing the returned JSON, but when calling the same API from postman the response was a valid JSON. after debugging and troubleshooting the problem, I've figured out what has caused it, simply the response content-length header always remains the same length of the original body length even after the HandleSuccessRequestAsync update the body, and the browser takes only the same number of characters that match the value of the content-length header from the returned body and tries to parse it which causes the problem.

I've just added the below line in finally scope to fix it: context.Response.ContentLength = memoryStream.Length;

proudmonkey commented 4 years ago

Hi,

Thank you for the feedback. Which version you were using? Also could you provide a sample to reproduce the issue and what is the actual error you are getting?

I'll definitely look into this.

nasserkhalifa commented 4 years ago

Hey,

I'm using the latest version, I have login API that returns JWT token and when I call the API from postman it returns with a completely valid response as JSON just like the below response :

{ "success": true, "statusCode": 200, "errorMessage": "", "apiResult": { "id": "3f276642-dc4e-4bac-a463-30a79c2ab44c", "accessToken": "eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJzeXNhZG1pbiIsImp0aSI6IjhkNDk0YmQ1LTY0MTUtNDgwMy05ZGUzLTRlZDM4OGIwOTcwYiIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWVpZGVudGlmaWVyIjoiM2YyNzY2NDItZGM0ZS00YmFjLWE0NjMtMzBhNzljMmFiNDRjIiwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS93cy8yMDA4LzA2L2lkZW50aXR5L2NsYWltcy9yb2xlIjoiVXNlciIsIm5iZiI6MTU4ODMxODQxNiwiZXhwIjoxNTkwOTEwNDE2LCJpc3MiOiJodHRwOi8veW91cmRvbWFpbi5jb20iLCJhdWQiOiJUcmF2ZWxBZ2VuY3kifQ.4LVKxmslMrWBmeZ9_ZecPYTvIuZ2oMzo-2chp7WS5cE" } }

but from my angular application, I've checked the network tab the returned JSON is like the below response :

{ "success": true, "statusCode": 200, "errorMessage": "", "apiResult": { "id": "3f276642-dc4e-4bac-a463-30a79c2ab44c", "accessToken": "eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJzeXNhZG1pbiIsImp0aSI6IjhkNDk0YmQ1LTY0MTUtNDgwMy05ZGUzLTRlZDM4OGIwOTcwYiIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWVpZGVudGlmaWVyIjoiM2YyNzY2NDItZGM0ZS00YmFjLWE0NjMtMzBhNzljMmFiNDRjIiwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS93cy8yMDA4LzA2L2lkZW50aXR5L2NsYWltcy9yb2xlIjoiVXNlciIsIm5iZiI6MTU4ODMxODQxNiwiZXhwIjoxNTkwOTEwNDE2LCJpc3MiOi

and that surely not a valid response, the error is : Error: Http failure during parsing for http://localhost/webapi/api/account/login

it seems that postman doesn't care so much about the content-length header, unlike the browser.

proudmonkey commented 4 years ago

Are you sure you are using AutoWrapper. The format that I'm seeing here is very different, not unless if you are using custom Api Response object?

nasserkhalifa commented 4 years ago

this sample has been taken from the previous response wrapper (RESTApiResponseWrapper.Core).

but the same error will appear if you used AutoWrapper

proudmonkey commented 4 years ago

Did you tried it with AutoWrapper or you just assumed? The context.Response.ContentLength has already been reassigned before writing the response back to the context. You can see it here: https://github.com/proudmonkey/AutoWrapper/blob/master/src/AutoWrapper/AutoWrapperMembers.cs#L254

nasserkhalifa commented 4 years ago

Yes, I did, then it must be another case I didn't get through it so much I assumed its the same case of the deprecated version (RESTApiResponseWrapper.Core).

proudmonkey commented 4 years ago

Would you be able to give me the basic steps to reproduce with the Version of AutoWrapper you are using annd not the deprecated (RESTApiResponseWrapper.Core)?