Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp Net Core Web Api POST Request.Body always length 0

I am working on a service that will receive a file using the HTTP Content-Type: application/octet-stream and Transfer-Encoding: chunked.

I was able to get the server to get the request DisableFormValueModelBinding and RequestSizeLimit.

But when I go to get the data from the Request.Body the length is always 0.

In Framework I would use something like request.Content.ReadAsStreamAsync(); But this doesn't seem to be an option for Core.

How am I supposed get the content of the stream coming from the client (Postman)?

Using Postman I tried the binary and form-data options for the body but neither ended up having a body once it reached the server. Reading through some of the documentation there was suggestions around creating a new formatter that used a MultipartReader. But this all looked to be based around having the multipart/form-data content-type, which I am not using. I also tried using curl to send the request but the results were the same.

Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseMvc();
    }
}

Controller

[HttpPost]
[RequestSizeLimit(2147483648)] // https://stackoverflow.com/questions/43305220/form-key-or-value-length-limit-2048-exceeded
[DisableFormValueModelBinding] // https://dotnetcoretutorials.com/2017/03/12/uploading-files-asp-net-core/
public void Upload()
{
    Request.EnableRewind();
    Stream body = Request.Body;
    Debug.WriteLine(body.Length);
}
like image 261
165plo Avatar asked Nov 06 '25 01:11

165plo


1 Answers

I read that article where you got the [DisableFormValueModelBinding] from. The whole point of that attribute is to stop ASP.NET from reading the body. So that would make sense why body.Length is 0. It simply hasn't been read yet (and you can't know the length until you've read the whole thing).

But you can read the Content-Length header of the request:

var length = Request.ContentLength;
like image 179
Gabriel Luci Avatar answered Nov 07 '25 16:11

Gabriel Luci



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!