将文件上传到Google云端硬盘时,多部分主体格式错误 [英] Malformed multipart body when uploading file to Google Drive

查看:74
本文介绍了将文件上传到Google云端硬盘时,多部分主体格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用他们的/上载端点.我一直收到格式错误的多部分正文.错误,即使我尝试将简单的纯文本格式上传为文件.

Having a bit of trouble trying to resolve the issue with file upload to Google Drive using their /upload endpoint. I keep getting Malformed multipart body. error even when I try to upload simple plain text as a file.

以下.net c#代码用于创建请求:

The following .net c# code is used to create the request:

string fileName = "test.txt";
string fileContent = "The quick brown fox jumps over the lazy dog";

var fileStream = GenerateStreamFromString(fileContent); // simple text string to Stream conversion
var streamContent = new StreamContent(fileStream);
streamContent.Headers.ContentType = new MediaTypeHeaderValue("text/plain");

var multiPartFormDataContent = new MultipartFormDataContent("not_so_random_boundary");
// rfc2387 headers with boundary
multiPartFormDataContent.Headers.Remove("Content-Type");
multiPartFormDataContent.Headers.TryAddWithoutValidation("Content-Type", "multipart/related; boundary=" + "not_so_random_boundary");
// metadata part
multiPartFormDataContent.Add(new StringContent("{\"name\":\"" + fileName + "\",\"mimeType\":\"text/plain\",\"parents\":[\"" + folder.id + "\"]}", Encoding.UTF8, "application/json"));
// media part (file)
multiPartFormDataContent.Add(streamContent);

var response_UploadFile = await httpClient.PostAsync(string.Format("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"), multiPartFormDataContent);

我记录了以下请求:

Method: POST,
RequestUri: 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart',
Version: 1.1,
Content: System.Net.Http.MultipartFormDataContent,
Headers: { Authorization: Bearer /*snip*/ Content-Type: multipart/related; boundary=not_so_random_boundary }

具有以下请求内容(已修饰):

with following request content (pretified):

--not_so_random_boundary
Content-Type: application/json; charset=utf-8

Content-Disposition: form-data
{"name":"test.txt","mimeType":"text/plain","parents":["/*snip*/"]}

--not_so_random_boundary
Content-Type: text/plain

Content-Disposition: form-data
The quick brown fox jumps over the lazy dog
--not_so_random_boundary--

我花了整整一整天的时间,这使我明白了这一点.我预感这个问题有些愚蠢,但我只是想不通.

I've spent the entire day on this and it got me to this point. I have a hunch that issue is something silly but I just can't figure it out.

有人可以将视线转移到这个问题上吗,也许您可​​以发现我犯了一个错误,这将是非常有帮助的?

Could someone throw their eyes over this perhaps you can spot where I made a mistake that would be very helpful?

发送分段上传请求

RFC 2387

推荐答案

我认为 multipart/related 的请求正文的结构可能不正确.那么如何进行如下修改?

I think that the structure of the request body for multipart/related might not be correct. So how about modifying as follows?

--not_so_random_boundary
Content-Type: application/json; charset=utf-8
Content-Disposition: form-data; name="metadata"

{"name":"test.txt","mimeType":"text/plain","parents":["/*snip*/"]}
--not_so_random_boundary
Content-Type: text/plain
Content-Disposition: form-data; name="file"

The quick brown fox jumps over the lazy dog
--not_so_random_boundary--

  • 请注意请求正文的换行符.
  • 请为 Content-Disposition 的每个部分添加 name .
    • Please be careful the line breaks for the request body.
    • Please add name for each part of Content-Disposition.
      • 现在我可以确认,当上述修改后的请求正文用作 https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart 的端点作为POST方法时,将创建一个文本文件 test.txt ,其内容为棕色狐狸跳过懒狗.
      • Now I could confirm that when above modified request body is used for the endpoint of https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart as POST method, a text file of test.txt which has the content of The quick brown fox jumps over the lazy dog is created.

      如果这不起作用,我深表歉意.

      If this didn't work, I apologize.

      这篇关于将文件上传到Google云端硬盘时,多部分主体格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆