Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Body Parser as Middleware #67

Open
alec1o opened this issue Oct 17, 2024 · 2 comments
Open

Body Parser as Middleware #67

alec1o opened this issue Oct 17, 2024 · 2 comments
Labels
Milestone

Comments

@alec1o
Copy link
Owner

alec1o commented Oct 17, 2024

Description:

HTTP.Body.Enctype is already detected and saved, allowing us to simplify parsing the body content. The method HTTP.Body.Parse<T>() will use this detected enctype to automatically parse the content.
HTTP.Body.OnParse(HTTP.Enctype enctype, bool replaceOnMatch, Func<Type, object>) Used to set parser configuration.

Key Points:

  • HTTP.Body.Enctype: Detected and saved separately.
  • HTTP.Body.Parse<T>(): Uses the saved enctype to parse content based on the type (e.g., JSON, form data).

Benefits:

  • Simplicity: One method to handle different enctype values.
  • Extensible: Easily add support for more enctype values.
  • Clean Parsing: Logic stays organized by using the already-detected enctype.
  • Extra: replaceOnMatch Make custom middleware rewrite/override Already set parser handler

Example Usage:

var server = new HTTP.Server();

server.Map.Post("/", (request, response) =>
{
    var user = request.Body.Parse<CreateUserDTO>();
    if (user == null) return response.Send(400, "{'message': 'invalid body'}");
    return response.Send(201);
});

server.To.Open(new Uri("https://127.0.0.1:8080"));

Usage middleware to parse HTTP.Body

var server = new HTTP.Server();

server.Middleware.Add((request, response, next) =>
{
    var text = request.Body.Text;
    
    // .NET 6+, also can use Json.NET (Newtonsoft Json)  or others implementation
    // Adding JSON Enctype parser
    request.Body.OnParse(HTTP.Enctype.Json, false, (type) => JsonSerializer.Deserialize(text, type, JsonSerializerOptions.Default));
    
    // Adding YAML Enctype parser
    request.Body.OnParse(HTTP.Enctype.Yml, false, (type) => Example.Method(text, type, ExampleOptions.Default));
    
    // Adding XML Enctype parser
    request.Body.OnParse(HTTP.Enctype.Xml, false, (type) => Example.Method(text, type, ExampleOptions.Default));
    
    ...   
    
    next();
});

server.To.Open(new Uri("https://127.0.0.1:8080"));
@alec1o alec1o added Features New feature or request ToDo and removed Review labels Oct 17, 2024
@alec1o
Copy link
Owner Author

alec1o commented Oct 19, 2024

Solved at: 6a79392

@alec1o alec1o added Solved Question/Ask solved. and removed ToDo labels Oct 19, 2024
@alec1o
Copy link
Owner Author

alec1o commented Oct 19, 2024

I @ale1o @AlecioFuranze (Alecio Furanze)
Currently, there are no built-in parsers for Enctype, but the mechanism to implement middleware for parsing is already in place. In future versions, we will add built-in parsers for Enctypes such as JSON, XML, and YAML. This will reduce the need to manually create Enctype parsers to process the request body. However, even after the addition of these integrated parsers, you can still choose to use a more sophisticated parser, and the library will continue to support these parsing options via middleware in case you need to parse unsupported Enctypes or for any other reason. The key difference is that the built-in parsers will be available by default.

Enctype Status
🌐 UrlEncoded ⏳ Planned
📂 Multipart ⏳ Planned
📄 Json ⏳ Planned
📜 Yaml ⏳ Planned
🗂️ Xml ⏳ Planned
📊 Csv ⏳ Planned
⚛️ GraphQL ⏳ Planned
📦 SoapXml ⏳ Planned
📁 MultipartRelated ⏳ Planned

Notes:

  • Planned: This Enctype is scheduled to be added in future versions with parsing support.
  • Built-in: This Enctype already has built-in parsing support in the library.

@alec1o alec1o added this to Netly Oct 19, 2024
@alec1o alec1o added this to the v5.0.0 milestone Oct 19, 2024
@alec1o alec1o added the ToDo label Nov 6, 2024
@alec1o alec1o added Soon and removed ToDo Solved Question/Ask solved. Features New feature or request labels Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

1 participant