Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mediaType null or empty when using RestSharp 108.0.2 with .NET 7 #1969

Closed
cheindel opened this issue Nov 9, 2022 · 19 comments
Closed

mediaType null or empty when using RestSharp 108.0.2 with .NET 7 #1969

cheindel opened this issue Nov 9, 2022 · 19 comments
Labels

Comments

@cheindel
Copy link

cheindel commented Nov 9, 2022

Bildschirm­foto 2022-11-09 um 22 55 56
Describe the bug

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

@cheindel cheindel added the bug label Nov 9, 2022
@alexeyzimarev
Copy link
Member

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.

@cheindel cheindel changed the title 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 Nov 9, 2022
@cheindel
Copy link
Author

cheindel commented Nov 9, 2022

@alexeyzimarev Sorry, I was in a rush. I hope it is more clear now.

@cheindel cheindel changed the title 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 Nov 11, 2022
@jhudsonWA
Copy link

jhudsonWA commented 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
Copy link

nesc58 commented Nov 15, 2022

When changing the TargetFrameworks of RestSharp to net7.0 the 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.

public StringContent(string content, Encoding? encoding, string? mediaType) : base(GetContentByteArray(content, encoding))
{
    // Initialize the 'Content-Type' header with information provided by parameters.
    MediaTypeHeaderValue headerValue = new MediaTypeHeaderValue((mediaType == null) ? DefaultMediaType : mediaType);
    headerValue.CharSet = (encoding == null) ? HttpContent.DefaultStringEncoding.WebName : encoding.WebName;
    Headers.ContentType = headerValue;
}

.net7.0 deimplements the check and changed the method signature (questionmark is removed)

public StringContent(string content, Encoding? encoding, string mediaType) : this(content, encoding, new MediaTypeHeaderValue(mediaType, (encoding ?? DefaultStringEncoding).WebName))
{
}

@alexeyzimarev
Copy link
Member

.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
Copy link
Member

Ok, basically the fix is to provide the default value for the Parameter.ContentType property.

@alexeyzimarev
Copy link
Member

The workaround for the current RS version would be to add the text/plain content type to POST parameters. It's an optional argument.

@jhudsonWA
Copy link

The workaround for the current RS version would be to add the text/plain content type to POST parameters. It's an optional argument.

Testing now, i’ll report back.

@jhudsonWA
Copy link

image

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
Copy link
Member

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 AddFile but I haven't seen anything breaking there.

@jhudsonWA
Copy link

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
Copy link
Member

@jhudsonWA you can start by looking at current tests that use AddFile.

@jhudsonWA
Copy link

jhudsonWA commented 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
Copy link
Author

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
Copy link

stale bot commented 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.

@stale stale bot added the wontfix label Jan 7, 2023
@repo-ranger
Copy link

repo-ranger bot commented Jan 7, 2023

⚠️ This issue has been marked wontfix and will be closed in 3 days

alexeyzimarev added a commit that referenced this issue Mar 12, 2023
* Added .NET Framework 471 as a target. .NET 5 is out
* Fix the media type issue with .NET 7 #1969
* `IRestClient` Interface is back
* String type for ContentType
* Added Authenticator to RestRequest
* Made Options immutable to make the client thread-safe
* Ignore exceptions parsing response cookies (#2015)
* V109 (#2010)
* Added a simple factory with opt-in

---------

Co-authored-by: Francesc Castells <fcastells76@gmail.com>
Co-authored-by: Francesc Castells <francesc.castells@sermo.com>
Co-authored-by: Edward Lichtman <E.Lichtman2@gmail.com>
@RudiSzalai
Copy link

Parameter.ContentType

Ok, basically the fix is to provide the default value for the Parameter.ContentType property.

How? Could someone be so kind to help with a piece of practical and useable advice?

@alexeyzimarev
Copy link
Member

alexeyzimarev commented 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
Copy link

the AddParameter doesn't have contentType. That's where I suck up. (the AddFile has)
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants