HttpClient returns unsupported media type error when I send json object along with IFormFile in .NET 6.0

Raz muhammad 1 Reputation point
2022-08-23T10:27:31.92+00:00

I have been struggling for the past three days trying to send serialized json of a complex type (DTO object called fileItem) and IFormFile type named as file to one of my API which accepts two inputs, a FileItem object and IFormFile object.

But I keep getting an error "Unsupported Media Type". Note that when I send only file or only object then to API end point by updating the endpoint to accept either file only or complexType only, then it works fine, but if I am sending both together, then I get that "Unsupported Media Type" error which is already available in the request I am sending

Please guide me in a proper direction your help will be really appreciated.

ASP.NET MVC web app

public async Task<string> SaveFiles(FileItem _fileItem2, IFormFile _file)  
{  
    using (var multipartFormContent = new MultipartFormDataContent())  
    {  
        var fileItem = new StringContent(  
        JsonSerializer.Serialize(_fileItem2), Encoding.UTF8, Application.Json);  
        multipartFormContent.Add(fileItem, "fileItem");  
  
        // Add the file  
        var fileStreamContent = new StreamContent(_file.OpenReadStream());  
        fileStreamContent.Headers.ContentType = MediaTypeHeaderValue.Parse(_file.ContentType);  
        multipartFormContent.Add(fileStreamContent, name: "file", _file.FileName);  
  
        // Method 1 : this method returns Unsupported media type, but if file is removed then it works fine.  
        var response = await httpClient.PostAsync("/Upload/SaveFileWithData", multipartFormContent);  
  
        // Method 2 : this method also returns Unsupported media type, but if file is removed then it works fine.  
        var request = new HttpRequestMessage(HttpMethod.Post, "/Upload/SaveFileWithData");  
        request.Content = multipartFormContent;  
        var responsemsg = await httpClient.SendAsync(request);  
  
        responsemsg.EnsureSuccessStatusCode();  
  
        return await responsemsg.Content.ReadAsStringAsync();  
    }  
}  

This is the .NET 6.0 Web API (above request we send to this api)

[HttpPost()]  
[Route("SaveFileWithData")]  
public async Task<string> SaveFileWithData(FileItem? fileItem, IFormFile? file)  
{  
    int i = 2;  
    return "fileSavedSuccessfully";  
}  
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,165 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,011 Reputation points Microsoft Vendor
    2022-08-24T03:26:03.847+00:00

    Hi @Raz muhammad ,

    You can modify your code as below and transfer the data via Form.

    FormItem model:

    234294-image.png

    MVC controller: add the model properties one by one.

    234230-image.png

    AddFileItem view page:

    234324-image.png

    API controller:

    234240-image.png

    You can view the above source code from here: 234295-sourcecode.txt

    Then, the output like this:

    234341-1.gif


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments