title: How to set the Content Type of the request in ASP.NET Core?
body: <p>I try to set the content type of a <code>multipart/form-data</code> GET request that miss boundary. I've tried this code:</p>
  
  <pre><code>public class BoundaryMiddleware
  {
      private readonly RequestDelegate _next;
  
      public BoundaryMiddleware(RequestDelegate next)
      {
          this._next = next;
      }
  
      public async Task Invoke(HttpContext context)
      {
          if (context.Request.Path.Value.Contains("api/webhook"))
          {  
              if (context.Request.ContentType == "multipart/form-data")
              { 
                  context.Request.ContentType = $"multipart/form-data; boundary=\"{Guid.NewGuid()}\""; 
              }
          }
  
          await _next.Invoke(context);
      }
  }
  </code></pre>
  
  <p>And the startup file: </p>
  
  <pre><code>public class Startup
  {
      public Startup(IConfiguration configuration)
      {
          Configuration = configuration;
      }
  
      public IConfiguration Configuration { get; }
  
      public void ConfigureServices(IServiceCollection services)
      {
          services.AddMvc();
      }
  
      public void Configure(IApplicationBuilder app, IHostingEnvironment env,
          ILoggerFactory loggerFactory,
          IConfiguration configuration)
      {
          loggerFactory.AddDebug();
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
              loggerFactory.AddDebug();
          }
  
          app.UseMiddleware&lt;BoundaryMiddleware&gt;();
  
          app.UseMvc();
      }
  }
  </code></pre>
  
  <p>However, this code throw the following Exception:</p>
  
  <pre><code> System.IO.IOException: Unexpected end of Stream, the content may have already been read by another component. 
       at Microsoft.AspNetCore.WebUtilities.MultipartReaderStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
       at Microsoft.AspNetCore.WebUtilities.StreamHelperExtensions.DrainAsync(Stream stream, ArrayPool`1 bytePool, Nullable`1 limit, CancellationToken cancellationToken)
       at Microsoft.AspNetCore.WebUtilities.MultipartReader.ReadNextSectionAsync(CancellationToken cancellationToken)
       at Microsoft.AspNetCore.Http.Features.FormFeature.InnerReadFormAsync(CancellationToken cancellationToken)
       at Microsoft.AspNetCore.Mvc.ModelBinding.FormValueProviderFactory.AddValueProviderAsync(ValueProviderFactoryContext context)
       at Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider.CreateAsync(ActionContext actionContext, IList`1 factories)
       at Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider.CreateAsync(ControllerContext controllerContext)
       at Microsoft.AspNetCore.Mvc.Internal.ControllerBinderDelegateProvider.&lt;&gt;c__DisplayClass0_0.&lt;&lt;CreateBinderDelegate&gt;g__Bind|0&gt;d.MoveNext()
  </code></pre>
  
  <p>Is there a way to modify this ? Thanks</p>
  
  <p>Edit: this is the resquest passed to the web app:</p>
  
  <pre><code>GET /api/webhook HTTP/1.1
  Host: localhost:5000
  content-type: multipart/form-data
  cache-control: no-cache
  </code></pre>
  
  <p>Edit 2: If I send the request as it, this exception is raised:</p>
  
  <pre><code>System.IO.InvalidDataException: Missing content-type boundary.
     at Microsoft.AspNetCore.Http.Features.FormFeature.GetBoundary(MediaTypeHeaderValue contentType, Int32 lengthLimit)
     at Microsoft.AspNetCore.Http.Features.FormFeature.InnerReadFormAsync(CancellationToken cancellationToken)
     at Microsoft.AspNetCore.Mvc.ModelBinding.FormValueProviderFactory.AddValueProviderAsync(ValueProviderFactoryContext context)
     at Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider.CreateAsync(ActionContext actionContext, IList`1 factories)
     at Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider.CreateAsync(ControllerContext controllerContext)
     at Microsoft.AspNetCore.Mvc.Internal.ControllerBinderDelegateProvider.&lt;&gt;c__DisplayClass0_0.&lt;&lt;CreateBinderDelegate&gt;g__Bind|0&gt;d.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
     at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
     at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
     at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
     at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
     at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
     at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
  </code></pre>