1

I'm using an ASP.NET Core Minimal API, the POST operation reads raw data:

app.MapPost("/Authenticate", async (HttpRequest Request) => {
    string rawContent = string.Empty;
    using (var reader = new StreamReader(Request.Body,
                    encoding: Encoding.UTF8, detectEncodingFromByteOrderMarks: false))
    {
        rawContent = await reader.ReadToEndAsync();
    }
    // .......
})
.WithName("Authenticate")
.WithOpenApi();

When I run dotnet run command and it opens the browser with the Swagger UI, the endpoint parameters are not available, the endpoint works perfect using Postman + raw post data, but it would be nicer if Swagger UI would allow me to input the raw post body too...

Thank you

Tried with interfaces as input, same result, empty parameters @ Swagger UI ...

12
  • Is there a particular why do you want to read the body manually via stream? Commented Mar 16, 2024 at 17:16
  • Do you know any other way to deal with raw data? Commented Mar 19, 2024 at 13:34
  • Is there any particular why do you want raw data here? Commented Mar 19, 2024 at 13:37
  • That's how the client will post the data using raw data... I have multiple clients / partners where their API works with raw data, nothing new.. or probably too classic in other programming languages... Commented Mar 20, 2024 at 16:26
  • So you are saying that your clients can post any type of JSON with any structure without any kind of contract? Commented Mar 20, 2024 at 16:32

1 Answer 1

1

You can customize the OpenAPI to include a requestBody schema that accepts raw data. Here is an example you can use as a reference:

app.MapPost("/Authenticate", async (HttpRequest Request) =>
{
    string rawContent = string.Empty;
    using (var reader = new StreamReader(Request.Body,
                    encoding: Encoding.UTF8, detectEncodingFromByteOrderMarks: false))
    {
        rawContent = await reader.ReadToEndAsync();
    }
    
})
.WithName("Authenticate")
.WithOpenApi(openApi =>
{
    openApi.RequestBody = new OpenApiRequestBody
    {
        Content = new Dictionary<string, OpenApiMediaType>
        {
            {
                "application/json", new OpenApiMediaType
                {
                    Schema = new OpenApiSchema
                    {
                        Type = "object" 
                    }
                }
            }
        },
        Description = "JSON"
    };

    return openApi;
});

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

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.