Repository navigation
Description
After changing target framework from .NET 6 to .NET 7 the following code in a Blazor server side application started to throw an exception (no matter if individual packages are referenced using their 6.0.10 or 7.0.0 versions):
var request = new RestRequest(BaseUrl + "waitinglist/" + accessToken);
request.AddParameter("officeNumber", PracticeId);
request.AlwaysMultipartFormData = true;
request.Method = Method.Post;
var response = client.ExecuteAsync(request);
Once I change target framework back to .NET 6, it works again.
To Reproduce
See description above and screenshot attached
Expected behavior
No exception but results returned.
Stack trace
Microsoft.AspNetCore.Components.Server.Circuits.RemoteRenderer: Warning: Unhandled exception rendering component: The value cannot be null or empty. (Parameter 'mediaType')
System.ArgumentException: The value cannot be null or empty. (Parameter 'mediaType')
at System.Net.Http.Headers.MediaTypeHeaderValue.CheckMediaTypeFormat(String mediaType, String parameterName)
at System.Net.Http.StringContent..ctor(String content, Encoding encoding, String mediaType)
at RestSharp.RequestContent.AddPostParameters(ParametersCollection postParameters)
at RestSharp.RequestContent.BuildContent()
at RestSharp.RestClient.ExecuteInternal(RestRequest request, CancellationToken cancellationToken)
at RestSharp.RestClient.ExecuteAsync(RestRequest request, CancellationToken cancellationToken)
at DPAForPRODUCTNAMEREMOVEDBlazor.Controllers.PRODUCTNAMEREMOVED.DownloadPRODUCTNAMEREMOVEDJSON(Boolean forceDownload) in /Users/christian/projects/Insights-and-Services-for-PRODUCTNAMEREMOVED/DPAForPRODUCTNAMEREMOVEDBlazor/Controllers/PRODUCTNAMEREMOVED.cs:line 297
at DPAForPRODUCTNAMEREMOVEDBlazor.Controllers.PRODUCTNAMEREMOVED.DownloadPRODUCTNAMEREMOVEDJSON() in /Users/christian/projects/Insights-and-Services-for-PRODUCTNAMEREMOVED/DPAForPRODUCTNAMEREMOVEDBlazor/Controllers/PRODUCTNAMEREMOVED.cs:line 198
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at MudBlazor.MudBaseButton.OnClickHandler(MouseEventArgs ev)
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]
Unhandled exception in circuit '3hFrvBILVAv_71c7LL_TtxPkV0PGJNviJWzjwzYOQgQ'.
System.ArgumentException: The value cannot be null or empty. (Parameter 'mediaType')
at System.Net.Http.Headers.MediaTypeHeaderValue.CheckMediaTypeFormat(String mediaType, String parameterName)
at System.Net.Http.StringContent..ctor(String content, Encoding encoding, String mediaType)
at RestSharp.RequestContent.AddPostParameters(ParametersCollection postParameters)
at RestSharp.RequestContent.BuildContent()
at RestSharp.RestClient.ExecuteInternal(RestRequest request, CancellationToken cancellationToken)
at RestSharp.RestClient.ExecuteAsync(RestRequest request, CancellationToken cancellationToken)
at DPAForPRODUCTNAMEREMOVEDBlazor.Controllers.PRODUCTNAMEREMOVED.DownloadPRODUCTNAMEREMOVEDJSON(Boolean forceDownload) in /Users/christian/projects/Insights-and-Services-for-PRODUCTNAMEREMOVED/DPAForPRODUCTNAMEREMOVEDBlazor/Controllers/PRODUCTNAMEREMOVED.cs:line 297
at DPAForPRODUCTNAMEREMOVEDBlazor.Controllers.PRODUCTNAMEREMOVED.DownloadPRODUCTNAMEREMOVEDJSON() in /Users/christian/projects/Insights-and-Services-for-PRODUCTNAMEREMOVED/DPAForPRODUCTNAMEREMOVEDBlazor/Controllers/PRODUCTNAMEREMOVED.cs:line 198
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at MudBlazor.MudBaseButton.OnClickHandler(MouseEventArgs ev)
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost: Error: Unhandled exception in circuit '3hFrvBILVAv_71c7LL_TtxPkV0PGJNviJWzjwzYOQgQ'.
System.ArgumentException: The value cannot be null or empty. (Parameter 'mediaType')
at System.Net.Http.Headers.MediaTypeHeaderValue.CheckMediaTypeFormat(String mediaType, String parameterName)
at System.Net.Http.StringContent..ctor(String content, Encoding encoding, String mediaType)
at RestSharp.RequestContent.AddPostParameters(ParametersCollection postParameters)
at RestSharp.RequestContent.BuildContent()
at RestSharp.RestClient.ExecuteInternal(RestRequest request, CancellationToken cancellationToken)
at RestSharp.RestClient.ExecuteAsync(RestRequest request, CancellationToken cancellationToken)
at DPAForPRODUCTNAMEREMOVEDBlazor.Controllers.PRODUCTNAMEREMOVED.DownloadPRODUCTNAMEREMOVEDJSON(Boolean forceDownload) in /Users/christian/projects/Insights-and-Services-for-PRODUCTNAMEREMOVED/DPAForPRODUCTNAMEREMOVEDBlazor/Controllers/PRODUCTNAMEREMOVED.cs:line 297
at DPAForPRODUCTNAMEREMOVEDBlazor.Controllers.PRODUCTNAMEREMOVED.DownloadPRODUCTNAMEREMOVEDJSON() in /Users/christian/projects/Insights-and-Services-for-PRODUCTNAMEREMOVED/DPAForPRODUCTNAMEREMOVEDBlazor/Controllers/PRODUCTNAMEREMOVED.cs:line 198
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at MudBlazor.MudBaseButton.OnClickHandler(MouseEventArgs ev)
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
Desktop (please complete the following information):
- OS: macOS Ventura 13.0.1
- .NET version .NET 7.0.100
- Version 108.0.2
Additional context
none

Activity
alexeyzimarev commentedon Nov 9, 2022
Sorry but the issue doesn't tell enough to diagnose the problem or even understand what the problem is. Please provide more details following the issue template.
[-]mediaType null or empty when using using RestSharp 107 or higher with .NET 7[/-][+]mediaType null or empty when using using RestSharp 108.0.2 with .NET 7[/+]cheindel commentedon Nov 9, 2022
@alexeyzimarev Sorry, I was in a rush. I hope it is more clear now.
[-]mediaType null or empty when using using RestSharp 108.0.2 with .NET 7[/-][+]mediaType null or empty when using RestSharp 108.0.2 with .NET 7[/+]jhudsonWA commentedon Nov 15, 2022
I get the same thing with the same version and .NET 7 using the AddFile method
request.AddFile("attachment", attachment.FileBytes, attachment.FileName);
Also tried passing a content type and same error when var response = client.ExecuteAsync(request); is called.
The value cannot be null or empty. (Parameter 'mediaType')
Happy to complete another issue, but the subject is the exactly the same, even if the method to get there is slightly different.
nesc58 commentedon Nov 15, 2022
When changing the TargetFrameworks of RestSharp to
net7.0the error occurs in MultipartFormDataTests as well.I have looked at the microsoft implementation and there are some changes.
.net6.0 implements a check to use the DefaultMediaType fallback when the given mediaType is null.
.net7.0 deimplements the check and changed the method signature (questionmark is removed)
alexeyzimarev commentedon Nov 15, 2022
.NET seven will be added as a direct target for v109 and will be executing tests for the target SDKs in the pipeline. We'll see what fails and what requires fixing.
Thanks for reporting. I haven't seen this particular one in the list of breaking changes. I guess, in time we might get more of those, unfortunately.
alexeyzimarev commentedon Nov 16, 2022
Ok, basically the fix is to provide the default value for the
Parameter.ContentTypeproperty.alexeyzimarev commentedon Nov 16, 2022
The workaround for the current RS version would be to add the
text/plaincontent type to POST parameters. It's an optional argument.Fix the media type issue with .NET 7 #1969
jhudsonWA commentedon Nov 16, 2022
Testing now, i’ll report back.
jhudsonWA commentedon Nov 16, 2022
This still fails with the mediaType error. I also tried to pass in the actual type "application/pdf" and same error. Again my issue is a different path to get the same error.
alexeyzimarev commentedon Nov 16, 2022
I haven't seen failed tests with files. I only reproduced the multipart form issue, and the workaround applies there.
I will double check the tests for
AddFilebut I haven't seen anything breaking there.jhudsonWA commentedon Nov 16, 2022
I’ll create a test project if I have to. This call with or without the type results in the same error me i fortunatly. :(
alexeyzimarev commentedon Nov 17, 2022
@jhudsonWA you can start by looking at current tests that use
AddFile.jhudsonWA commentedon Nov 18, 2022
Ok, I was barking up the wrong tree. AddFile does work great. AddFile on a request along with other post data is a Multipart Form Data situation and would run into this bug. I tested adding the latest 109 (compiled from the source) and the issue is confirmed fixed.
cheindel commentedon Nov 21, 2022
I can confirm that the issue in my project is resolved with RestSharp 109.0.0-preview.1
Thank you for the quick fix @alexeyzimarev !
stale commentedon Jan 7, 2023
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
repo-ranger commentedon Jan 7, 2023
v109 (#1963)
RudiSzalai commentedon Mar 27, 2023
How? Could someone be so kind to help with a piece of practical and useable advice?
alexeyzimarev commentedon Mar 28, 2023
It's shown right there #1969 (comment)
It depends on the parameter type which fails with null media type in your code.
RudiSzalai commentedon Mar 28, 2023
the AddParameter doesn't have contentType. That's where I suck up. (the AddFile has)
