How to fix 415 Unsupported Media Type

Eddy Boughioul 0 Reputation points
2023-07-14T10:04:57.6+00:00

Hello,

I'm working on asp.net core 7 website with angular as front.

I have a form who contains data and files, so I sent it as multipart form-data, in local the forms works but once the website is deployed on Azure as app service I got an error :

{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"00-c3d5eb1b571a0222546e2172dec68cf6-2de4d4301e7c6b54-00"}

How I sent my data in angular :

 public publish(body: any) {     const formData = new FormData();     for (let key in body) {       if (key === "pictures") {         const files = body[key] as File[];         if (files?.length > 0) {           files.forEach((f, i) => formData.append(key, f));         } else {           formData.append(key, null);         }       } else {         formData.append(key, body[key]);       }     }        return this.http.post
Developer technologies | ASP.NET | ASP.NET Core
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,976 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-07-17T12:18:59.0966667+00:00

    Hi @Eddy Boughioul,

    Welcome to Q&A, could you kindly check other apis, I mean something like /weatherforecast, you also could create one for test. I need to know it works or not? If it works and just that api returned 415 error.

    We need the check the size of form-data in your request. Maybe it will lead to this error.

    The default settings in Azure App Service might have a lower limit on request size or not be configured to handle multipart form-data requests by default. You can change it like below. Here is my Code sample.

                var builder = WebApplication.CreateBuilder(args);
    
                // Add services to the container.
                builder.Services.AddControllersWithViews();
                ...
                // Add this setting
                builder.Services.Configure<FormOptions>(options =>
                {
                    options.MemoryBufferThreshold = int.MaxValue;
                    options.MultipartBodyLengthLimit = int.MaxValue;
                });
                var app = builder.Build();
    
                // Configure the HTTP request pipeline.
                if (!app.Environment.IsDevelopment())
                {
                    app.UseExceptionHandler("/Home/Error");
                    // 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.UseStaticFiles();
                // Add this middleware
                app.Use(async (context, next) =>
                {
                    context.Request.EnableBuffering();
                    await next();
                });
    
                app.UseRouting();
    
                app.UseAuthorization();
    
                app.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
    
                app.Run();
    

    If it still not works for you, could you create the minimal sample for us and please hide your sensitive information. Then we can help you to check the issue.

    Best Regards

    Jason

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.