165plo
165plo

Reputation: 793

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);
}

Upvotes: 4

Views: 6076

Answers (1)

Gabriel Luci
Gabriel Luci

Reputation: 40928

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;

Upvotes: 8

Related Questions