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

In multipart form, JSON part is missing the JSON content type in the content disposition header #1716

Closed
kbilkhoo opened this issue Jan 21, 2022 · 14 comments
Labels
awaiting-feedback Need feedback after the issue is fixed in preview bug

Comments

@kbilkhoo
Copy link

kbilkhoo commented Jan 21, 2022

Describe the bug
Request.Form containd no data

To Reproduce

client side

    var request = new RestRequest(resource);
    var jsonData = new
    {
            Company = "Microsoft",
            ZipCode = "LS339",
            Country = "USA"

    };

    request.RequestFormat = DataFormat.Json;
    request.Method = Method.Post;
    request.AddHeader("Content-Type", "multipart/form-data");
    request.AddFile("content", file, "application/octet-stream"); 
    request.AddJsonBody(jsonData);


    var response = client.ExecuteAsync(request).Result;

server side

   var request = HttpContext.Current.Request;
   Request.Form[0]; // index out of range exception

Expected behavior

    Request.Form[0]; // expected data

Stack trace
There is not stack trace for this issue. Except behaviour has changed
after upgrading from 106.13.0 to 107.1.1

106.13.0 -> Request.Form contains key called 'application/json'
107.1.1 -> Request.Form no data

Desktop (please complete the following information):

OS: Windows 10
.NET version 4.7.2
Version: 107.1.1
Additional context

workaround

changed request.AddJsonBody(jsonData) ->request.AddParameter("application/json", JsonConvert.SerializeObject(jsonData));
has resolved the issue in 107.1.1

@kbilkhoo kbilkhoo added the bug label Jan 21, 2022
@kbilkhoo
Copy link
Author

All working now, changed the line

request.AddJsonBody(dataToPost) ->  request.AddParameter("application/json", JsonConvert.SerializeObject(dataToPost));

request.Form now contain the posted json data

@alexeyzimarev
Copy link
Member

Have you followed the bug report issue template? We ask to trace both working and non-working requests, so it's possible to see the difference.

@kbilkhoo
Copy link
Author

Sorry I did not, but can do later on if you wish

@alexeyzimarev
Copy link
Member

Here is what I found.

RS 106 JSON data:

Content-Type: application/json.
Content-Disposition: form-data; name="application/json".
.
{"Company":"Microsoft","ZipCode":"LS339","Country":"USA"}.

RS 107:

Content-Type: application/json; charset=utf-8.
Content-Disposition: form-data.
.
{"company":"Microsoft","zipCode":"LS339","country":"USA"}.

Apparently, the content type for the JSON form part is not set properly in the content disposition header. I'll look into it.

@alexeyzimarev alexeyzimarev changed the title Multi-part request with JSON body, failed in 107.1.1, no form part In multipart form, JSON part is missing the JSON content type in the content disposition header Jan 22, 2022
@alexeyzimarev
Copy link
Member

No, I think I wrote some rubbish. The content type is correct. The only difference is that the part name is not set to "application/json", which doesn't seem to be any kind of requirement.

Nevertheless, I must've been there for a reason to use the body parameter content type as the content disposition form part name. Looks weird to me, but who knows. The 107.1.2-alpha.0.10 sets it as before, you might want to try it out.

@alexeyzimarev alexeyzimarev added the awaiting-feedback Need feedback after the issue is fixed in preview label Jan 22, 2022
@kbilkhoo
Copy link
Author

Hi, Upgraded to 107.1.2-alpha.0.18 now I get a different error, when calling client.ExecuteAsync(request).Result;

Message = "The type initializer for 'RestSharp.HttpContentExtensions' threw an exception."

Message = "Instance field '_boundary' is not defined for type 'System.Net.Http.MultipartContent'"

at RestSharp.HttpContentExtensions.GetFormBoundary(MultipartFormDataContent content)
at RestSharp.RequestContent.GetContentTypeHeader(String contentType)
at RestSharp.RequestContent.g__AddHeader|15_1(Parameter parameter)
at RestSharp.Extensions.CollectionExtensions.ForEach[T](IEnumerable1 items, Action1 action)
at RestSharp.RequestContent.AddHeaders()
at RestSharp.RequestContent.BuildContent()
at RestSharp.RestClient.d__1.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at RestSharp.RestClient.d__0.MoveNext()

@alexeyzimarev
Copy link
Member

It seems to be platform-specific. I expected it, the old code is still in place. The issue is that the MultipartFormBoundary class doesn't expose the boundary property, although they have it inside. The current implementation accesses it via reflections, but I thought it might not work on some platforms.

I am considering replacing the boundary generated by the content class with another GUID that I can keep and access. It should resolve the issue.

@alexeyzimarev alexeyzimarev removed the awaiting-feedback Need feedback after the issue is fixed in preview label Jan 24, 2022
@alexeyzimarev
Copy link
Member

And, I think you are not on .NET 5 or 6, right?

@kbilkhoo
Copy link
Author

correct, we are currently using .NET 4.7.2 to test the API,

@alexeyzimarev
Copy link
Member

Ok, it's better than I thought. It's possible to provide the boundary value instead of letting the content generate it.

@alexeyzimarev
Copy link
Member

I removed all the stuff with reflections, plus it's now possible to override the boundary if necessary. All good. Please try the next alpha when it gets published, it will take a few minutes.

@alexeyzimarev
Copy link
Member

I expect that after all the changes those requests will be identical, except the charset in the content type. If it still won't work, you can try eliminating the charset by setting request.DisableCharSet = true

@alexeyzimarev alexeyzimarev added the awaiting-feedback Need feedback after the issue is fixed in preview label Jan 25, 2022
@alexeyzimarev
Copy link
Member

Any news?

@kbilkhoo
Copy link
Author

all works as expected, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-feedback Need feedback after the issue is fixed in preview bug
Projects
None yet
Development

No branches or pull requests

2 participants