-
Notifications
You must be signed in to change notification settings - Fork 260
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
Multipart form-data boundary is missing from Content-type header #92
Comments
Having the exact same issue, 2.0.1 works fine, while the latest 2.1.2 is missing this part exactly. Take this for example: public async void PostFile(string url, string fileNameLocal, CancellationToken cancellationToken)
{
var fileName = Path.GetFileName(fileNameLocal);
using (var client = new HttpClient(new NativeMessageHandler()))
{
var fileStream = File.Open(fileName, FileMode.Open, FileAccess.Read);
var streamContent = new StreamContent(fileStream);
streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
streamContent.Headers.ContentDisposition.Name = "\"file\"";
streamContent.Headers.ContentDisposition.FileName = "\"" + fileName + "\"";
streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
string boundary = "12345";
var fContent = new MultipartFormDataContent(boundary);
fContent.Headers.Remove("Content-Type");
fContent.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary);
fContent.Add(streamContent);
var response = await client.PostAsync(new Uri(url), fContent, cancellationToken);
response.EnsureSuccessStatusCode();
streamContent.Dispose();
fileStream.Dispose();
}
} |
That took me a day to try to force the boundar. I also tried to use OkhttpClient directly: I got the boundary, but I lost the Content-Length, and that Amazon S3 doesn't like. So at the end I moved back to 2.0.1, now I works, at least I can upload images on S3. Thank you @rogihee |
Can someone create a repro project that shows the bug? http://httpbin.org might be helpful here |
Hi Paul, don’t have a complete sample, but here is some code for posting a file to an url in multi-part form that is working in 2.0, but not in the latest version: public async void PostRequestFileImpl(string url, string localFileName, CancellationToken cancelToken, Action<string, Exception> result)
I was already attempting in fixing it, and was trying something in the SendAsync method in the OkNetworkHttHandler: var body = default(RequestBody);
I’m too unfamiliar with Ok for this to completely understand / grasp, but this was the general direction I was looking for a possible solution. Best regards, Rogier From: Paul Betts [mailto:[email protected]] Can someone create a repro project that shows the bug? http://httpbin.org might be helpful here — |
@rogihee Cool - can you create a PR that modifies Playground.Android to show this bug, using this code above? |
I'm on it |
@rogihee Any progress on this? |
Was about to PM you but it's gonna be the weekend, too busy in the day job. I checked it in iny private repo though, but really needs testing before doing PR. |
@rogihee Whatever you've got is fine, you can always make more commits to continue work on a PR. You should always open PRs when you start working on something, not when you finish it! |
This is fixed! Huge thanks to @rogihee for the repro app. |
Glad you found the bug! Thanx for fixing it ;-) |
Thanks for picking this up @paulcbetts and @rogihee! |
@rogihee could you please guide me on this "https://stackoverflow.com/users/8875271/radhey-g" |
Something I'm noticing since the ModernHttpClient 2.x series is that the boundary is missing from the Content-type header when using Multipart form-data.
I'm using a derivate class from System.Net.Http.MultipartContent which has an optional boundary parameter in it's constructor. When this isn't set HttpClient generates one automatically. This boundary is then added between all the content parts, and added as a parameter to the Content-type like this:
"multipart\/form-data; boundary=\"379dc741-a272-4441-b777-c892aa3cd45d\""
The Content-type using ModernHttpClient looks like this:
"multipart\/form-data"
I've tried setting a boundary myself in the constructor of MultipartContent, but this doesn't make any difference.
When dumping Content.Headers.ContentType.ToString () from the request, it correctly outputs the header including the boundary, so it's kind of surprising it's missing from the actual request being sent. It's as if only the MediaType property is being sent, while the Parameters property is completely ignored.
The text was updated successfully, but these errors were encountered: