Skip to content

AddPostParameters with MultiPartFormDataContent throws error The value cannot be null or empty. (Parameter 'mediaType') #2000

Closed
@tim-arheit

Description

@tim-arheit

This appears to be an issue in version 108.0.3 with .Net 7.

In AddPostParameters when Content is of type MultiPartFormDataContent it attempts to add a new StringContent(), but the postParameter.ContentType is null or empty. Had to revert back to version 106.15.0 to get it working as a quick fix. I think, but haven't confirmed that 108.0.3 was not an issue with .net 6. It's possible I may be missing something, but the only change to our production code was an update of .net and RestSharp.

Based on a simple test it seems to be erroring out around line 158 of RestRequest.cs in AddPostParameters on newStringContent() which is:

 void AddPostParameters(ParametersCollection? postParameters) {
    if (postParameters.IsEmpty()) return;

    if (Content is MultipartFormDataContent mpContent) {
        // we got the multipart form already instantiated, just add parameters to it
        foreach (var postParameter in postParameters!) {
            var parameterName = postParameter.Name!;

            mpContent.Add(
                new StringContent(postParameter.Value!.ToString()!, _client.Options.Encoding, postParameter.ContentType),
                _request.MultipartFormQuoteParameters ? $"\"{parameterName}\"" : parameterName
            );
        }
    }
    else { ....

The parameter types in my case were GetOrPostParameter and UrlSegmentParameter, both of which do not set the ContentType, which appear to not be optional for new StringContent() (ContentType can now only be set in the constructor) There are no issues when Content is not MultipartFormDataContent.

Sample test code:

  [Test]
    public void RestSharp_Attachment_Test()
    {
        RestClient client = new("https://api.mailgun.net/v2")
        {
            Authenticator = new RestSharp.Authenticators.HttpBasicAuthenticator("api", "xyz")
        };


        RestRequest request = new();
        request.AddParameter("domain", "mydomain.com", ParameterType.UrlSegment);
        request.Resource = "{domain}/messages";

        request.AddParameter("to", "me@test.com");
        request.AddParameter("subject", "Test Email");
        request.AddParameter("text", "Body");

        byte[] data = Encoding.ASCII.GetBytes("Test text file");
        request.AddFile("textFile.txt", data, "text/plain");

        request.Method = Method.Post;
        RestResponse response = client.Execute(request);

    }

Error that is thrown:

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 RestSharp.AsyncHelpers.<>c__DisplayClass1_01.<<RunSync>b__0>d.MoveNext() --- End of stack trace from previous location --- at RestSharp.AsyncHelpers.CustomSynchronizationContext.<Run>g__PostCallback|7_0(Object _) at RestSharp.AsyncHelpers.CustomSynchronizationContext.Run() at RestSharp.AsyncHelpers.RunSync(Func1 task)
at RestSharp.AsyncHelpers.RunSync[T](Func`1 task)
at RestSharp.RestClient.Execute(RestRequest request, CancellationToken cancellationToken)
at Velocity.Common.Test.Utility.Email_Test.RestSharp_Attachment_Test() in C:\dbs\velocity\Blocks\Velocity\Common.Test\Utility\Email_Test.cs:line 66
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

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