Skip to content

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

Closed

Description

@cheindel

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

Activity

  1. alexeyzimarev commented on Nov 9, 2022

    @alexeyzimarev
    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.

  2. 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[/+] on Nov 9, 2022
  3. cheindel commented on Nov 9, 2022

    @cheindel
    Author

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

  4. 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[/+] on Nov 11, 2022
  5. jhudsonWA commented on Nov 15, 2022

    @jhudsonWA

    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.

  6. nesc58 commented on Nov 15, 2022

    @nesc58

    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))
    {
    }
    
  7. alexeyzimarev commented on Nov 15, 2022

    @alexeyzimarev
    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.

  8. alexeyzimarev commented on Nov 16, 2022

    @alexeyzimarev
    Member

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

  9. alexeyzimarev commented on Nov 16, 2022

    @alexeyzimarev
    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.

  10. jhudsonWA commented on Nov 16, 2022

    @jhudsonWA

    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.

  11. jhudsonWA commented on Nov 16, 2022

    @jhudsonWA

    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.

  12. alexeyzimarev commented on Nov 16, 2022

    @alexeyzimarev
    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.

  13. jhudsonWA commented on Nov 16, 2022

    @jhudsonWA

    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. :(

  14. alexeyzimarev commented on Nov 17, 2022

    @alexeyzimarev
    Member

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

  15. jhudsonWA commented on Nov 18, 2022

    @jhudsonWA

    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.

  16. cheindel commented on Nov 21, 2022

    @cheindel
    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 !

  17. stale commented on Jan 7, 2023

    @stale

    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.

  18. repo-ranger commented on Jan 7, 2023

    @repo-ranger

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

  19. added a commit that references this issue on Mar 12, 2023
  20. RudiSzalai commented on Mar 27, 2023

    @RudiSzalai

    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?

  21. alexeyzimarev commented on Mar 28, 2023

    @alexeyzimarev
    Member

    It's shown right there #1969 (comment)

    It depends on the parameter type which fails with null media type in your code.

  22. RudiSzalai commented on Mar 28, 2023

    @RudiSzalai

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions