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