Git Product home page Git Product logo

aspnetcorerequestresponsemiddlewaredemo's Introduction

My GitHub Stats

Hello Dear Readers!

I'm Matthew Jones, a lead software developer, blogger, and teacher. I write the blog Exception Not Found, where I explain programming concepts in simple terms and always include sample code.

I've been blogging for 5+ years, mostly in the .NET and ASP.NET space. If you're interested in these kinds of things, check out my posts on ASP.NET Core and C#, or the Categories page on my site.

I'm also a huge fan of board games, and model them in C# to show how to think about entire systems in terms of programming and software architecture. This series is called Modeling Practice.

BlazorGames.net

I also run another small project, BlazorGames.net, where we build computer and board games in Blazor WebAssembly and then show you, dear readers, how to do the same! I welcome any feedback anyone has about this project, and the GitHub repository is right over here.

The games already coded for this site include:

Where You Can Find Me

Twitter LinkedIn

What I Do

C# C# and .NET HTML JavaScript Web Development

What I Use

Visual Studio Visual Studio Code jQuery Bootstrap DigitalOcean NuGet

I ♥ Pull Requests!

As a matter of fact, ALL of my repositories are open and available for pull requests, and I almost always accept them. Feel free to improve on any of my projects! All code in these projects is availble free for all uses with no attribution required, except where that is legally impossible, and in those places they are licensed until the MIT license.

Support Me and My Projects!

If any of my projects have helped you, I would very much appreciate it if you would buy me a coffee.

Recent Blog Posts

aspnetcorerequestresponsemiddlewaredemo's People

Contributors

blackduck010 avatar exceptionnotfound avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

aspnetcorerequestresponsemiddlewaredemo's Issues

Errors for post requests

Hello,

I checked your code and i ran into an error during "post" messages.
The request body was empty resulting in the following error:
A non-empty request body is required.

I changed the code in the FormatRequest method:

  • copy of the request body is not necessary (enabled by enableRewind())
  • reset the stream to the start: request.Body.Seek(0, SeekOrigin.Begin);
    => then it worked like a charm :-)

Just wanted to let you know.

Regards,

Nahim Alhyane
Belgium

Code:

//This line allows us to set the reader for the request back at the beginning of its stream.
request.EnableRewind();

        //We now need to read the request stream.  First, we create a new byte[] with the same length as the request stream...
        var buffer = new byte[Convert.ToInt32(request.ContentLength)];

        //...Then we copy the entire request stream into the new buffer.
        await request.Body.ReadAsync(buffer, 0, buffer.Length);

        //We convert the byte[] into a string using UTF8 encoding...
        var bodyAsText = Encoding.UTF8.GetString(buffer);

        // Reset the stream to start:
        request.Body.Seek(0, SeekOrigin.Begin);
        
        return $"{request.Scheme} {request.Host}{request.Path} {request.QueryString} {bodyAsText}";

This doesn't seem to work with ASP.NET Core 3

I was playing around with this a bit and I cannot get this to work on ASP.NET Core 3.1

The middleware will read the request just fine but it seems to put the httpContext.Request.Body stream in an incorrect state so that my controller is not able to read the payload.

I get errors like this:
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"|c0a5172b-4ec228be2f2ccd8f.","errors":{"$":["The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0."]}}

I thought maybe the issue was that I was also utilizing Serilog but I disabled that and it still produces the same error.

The FormatResponse function returns an unusual string

Hello!

I am returning an unformatted string by the FormatResponse function.

Specifically, I am using Ocelot as the gateway and using Middleware as in the instruction to save logs.

When I call SOAP service, the information is still displayed on POSTMAN, but the log is not as expected

I also tried if I called RESful, all information is normal, SOAP only has this error

Hope you can help yourself. Thank you

This is the postman show
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <GetCustomerInfoResponse xmlns="http://tempuri.org/"> <GetCustomerInfoResult xmlns:a="http://schemas.datacontract.org/2004/07/PaymentGateway" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <a:Address> Ấp Đông Thuận, xã Thành An, H.Mỏ Cày</a:Address> <a:Bills> <a:BillInfo> <a:Amount>236399</a:Amount> <a:BillCode>210210TD</a:BillCode> <a:DenNgay i:nil="true"/> <a:HoaDonID>XXXXXXXXXX</a:HoaDonID> <a:Month>2</a:Month> <a:SoHo>0</a:SoHo> <a:TuNgay i:nil="true"/> <a:TyLeGia i:nil="true"/> <a:Year>2021</a:Year> </a:BillInfo> </a:Bills> <a:CustomerCode>XXXXXXXXXXXX</a:CustomerCode> <a:DanhSo>2137508</a:DanhSo> <a:MaSoThue/> <a:MaTram> </a:MaTram> <a:Name>Phạm Thị Nào</a:Name> <a:NganhNghe> </a:NganhNghe> <a:PhienGCS> </a:PhienGCS> <a:Session> </a:Session> <a:SoCongTo>XXXXXXXX</a:SoCongTo> <a:SoGhiChiSo>L1910-005</a:SoGhiChiSo> </GetCustomerInfoResult> </GetCustomerInfoResponse> </s:Body> </s:Envelope>

The middleware return value is as follows

\ u001F \ uFFFD \ b \ u0000 \ u0000 \ u0000 \ u0000 \ u0000 \ u0004 \ u0000 \ uFFFD \ uFFFD \ u0007 \ u0060 \ u001CI \ uFFFD% \ u0026 / m \ uFFFD {\ u007FJ \ uFFFDJ \ uFFFD \ uFFFDtt uFFFD \ b \ uFFFD \ u0060 \ u0013 $ \ u0610 @ \ u0010 \ uFFFD \ uFFFD \ uFFFD \ uFFFD \ uFFFD \ uFFFD \ u001DiG #) \ uFFFD * \ uFFFD \ uFFFDeVe] f77C \ uDFF \ uD uFFFD {\ uFFFD \ uFFFD \ uFFFD {\ uFFFD \ uFFFD \ uFFFD; \ uFFFDN \ u0027 \ uFFFD \ uFFFD \ uFFFD? \\ fd \ u0001l \ uFFFD \ uFFFDJ \ uFFFD \ uFFD \ uFFF001 \ uFFF ~ | \ u001F? \ u0022 \ u001E7 \ uFFFDN \ uFFFD \ uFFFDyY \ uFFFD \ uFFFD \ uFFFD \ u0762 \\ 6 \ uFFFD \ uFFFD \ uFFFD \ u003E \ uFFFD \ uFFFD \ uFFF77D \ uDFFFD \ uFFFD / \ uFFFDfL \ uFFFD7U \ uFFFD \ u001AW \ uFFFD \ uFFFD] \ uFFFDr7 \ uFFFD7 \ uFFFD ~ tD \ uFFFD? \ uFFFDf \ uFFFDG \ uFFFD \ uFFFD \ uFF3u \ uFFFD uFFFDfU- \ u001B \ u0005jA \ uFFFD \ uFFFDb \ uFFFD \ uFFFD \ u000B \ u0006E / \ uFFFD_Z \ uFFFD \ uFFFD \ uFFFD \ uFFFD (\ uFFFD / \ u0019 \ u003CfY \ uFFFeD! \ uFFFD \ uFFFD \ uFFFD \ uFFFDyp \ uFFFDev \ uFFFD \ u0217 \ uFFFD \ uFFFDY \ uFFFD_e \ u05E7? \ uFFFD \ uFFFD \ uFFFD \ u02D3 \ uFFFD \ u0014La] \ uFFFD \\ uFFFD \ uFFFD \ uFFFD_ \ u003C \ u007F \ u0350 \ uFFFD \ uFFFDe \ uFFFDf \ uFFFDiN \ u0018e \ uFFFD \ uFFFDg \ uFFFD: o \ uFFFD \ uFFFD \ uFFF \ uFFF \ uFFF \ uFFF uFFFD \ uFFFD \ u07F6 \ uFFFDH \ uFFFD \ uFFFD \ uFFFD \ uFFFD \ uFFFD \ uFFFD \ uFFFD \ uFFFD / G \ uFFFD \ u007F \ uFFFD / \ uFFFD \ uFFFD \ uDFF \ uDFF \ uD uFFFD \ uFFFD \ uFFFD \ uFFFD? 1 = \ uFFFDG \ uFFFD \ uFFFD \ uFFFD \ uFFFDw \ u077B \ u0004 \ uFFFDIQ \ uFFFD \ uFFFD \ u0017 \ f \ u000E \ uFFFD \ uDFFF / \ uFFFF \ uFFFD \ u9F47 \ u000F \ uFFFD \ u0005 \ uFFFD \ uFFFD \ uFFFDB \ uFFFD \ uFFFDj \ uFFFD \ u001F \ uFFFD \ uFFFD \ uFFFD \ uFFFD \ uFFFD \ uFFFD \ u003C \ uFFFD \ uFFFF \ u022E \ uFFFD \ uFFFD \ u0472 (? \ uFFFD \ uFFFD \ uFFFD \ uFFFD \ uFFFDGw \ uFFFD \ u0177 \ uFFFD \ uFFFDi \ uFFFD \ u003C {z \ uFFFDp \ uFFFD \ uDFFFD \ uFF \ uFFFD \ uFFFD \ uFFFDZ | AD \ uFFFD \ u001F \ uFFFD \ uFFFDc \ uFFFD \ uFFFD \ u003E {]} \ uFFFD: \ uFFFD \ uFFFDG \ uFFFy \ u001B} \ uFFFDf \ u001D \ uFFFD \ uFFD uFFFDy \ uFFFD \ uFFFD \ uFFFD \ uFFFD} \ uFFFD \ u003E \ uFFFD # l \ u0001 \ uFFFD \ uFFFD \ uFFFD / \ uFFFDY \ sdAB \ u007F0 \ u0015 \ uFFFD4 \ uFFFDh ^ \ u003E \ uFFFDyH \ uFFFD \ uFFFD \ uFFFD \ u003E \ uFFFD \ uFFFD \ u007F \ uFFFDf \ uFFFD \ uFFFD \ uFFFD \ uFFFDi \ uFFFD \ uFFFD \ uFFFD \ uDFFF \ uDFF u000B \ uFFFD \ uFFFD \ uFFFD \ uFFFD \ u0022 {] \ uFFFDt \ uFFFD \ uFFFD \ u0017 \ u065B: [\ u001C \ uFFFDh \ uFFFD \ uFFFD \ u04E7 / \ uFFFD \ uFFDuFF \ uFFF \ uFFFD \ uFFFD \ u001F \ uFFFD \ uFFFD \ u007Fl \ uFFFD \ uFFFD \ u001F \ uFFFD \ uFFFD * \ uFFFD \ uFFFDo \ uFFFD \ uFFFD \ u0F78 \ uFFFD \ uFFFD \ uFFFD \ uFFFD \ uFFFD \ uFFF \ uFFFD \ uFFFD \ u0027 \ uFFFD \ uFFFD \ u002B \ uFFFD \ u0017} \ uFFFD \ u6DA8 \ uFFFD \ uFFFD \ uFFFD \ u0003 \ uFFFDW \ u0027 \ uFFFD \ uFFFD \ uFFFD \ uFF u00C7 \ uFFFD; \ uFFFD \ uFFFD \ uFFFD \ uFFFDCn \ uFFFD \ uFFFD \ uFFFD8 \ uFFFD \ u00174 \ uFFFD \ uFFFD \ uFFFD \ u0060 {g \ u7FB4 \ uFFFD \ uFFF001 \ uFFF uFFFD, Y \ uFFFD = \ uFFFD \ uFFFD \ b \ uFFFD \ uFFFD \ uFFFD \ u0003 \ uFFFD \ f = B \ uFFFD \ u0003 \ u0000 \ u0000

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.