简体   繁体   English

使用RestSharp在multipart / form-data POST中包含文件的问题

[英]Trouble including a file in multipart/form-data POST using RestSharp

I'm relatively new to web interactions with C# and I'm having some trouble making a POST request to upload a file using an API. 我是与C#进行网络交互的新手,在发出POST请求以使用API​​上传文件时遇到了一些麻烦。 The API only accepts the files as part of a multipart/form-data section in the body. API仅接受文件作为正文中multipart / form-data节的一部分。 At other's suggestions I've been trying to use RestSharp to do this, but I can't seem to get the file itself into the POST. 根据其他人的建议,我一直在尝试使用RestSharp来执行此操作,但是我似乎无法将文件本身放入POST。 Chunks of code derived from Postman suggested code - where the POST works. 大量来自Postman建议代码的代码-POST工作的地方。

I've tried a few things. 我已经尝试了几件事。 This chunk resulted in a POST going through with the correct parameter in the body, but no file was included. 此块导致POST的正文中带有正确的参数,但未包含任何文件。

var client = new RestClient(postURL);
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", string.Format("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"upfile\"; filename=\"{0}\"\r\nContent-Type: application/xml\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"overwrite\"\r\n\r\ntrue\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", xmlPath), ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
string test = response.Content.ToString();

I also tried some variations of AddFile - physical file path, byte array, and byte array with content type = application/xml. 我还尝试了AddFile的一些变体-物理文件路径,字节数组和内容类型为application / xml的字节数组。 With these iterations, I was able to get a file to post, but the overwrite parameter wasn't coming through correctly to force a file overwrite. 通过这些迭代,我能够获得要发布的文件,但是未正确通过overwrite参数来强制覆盖文件。

var client = new RestClient(postURL);
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
// The different part below
request.AddFile("upfile", @xmlPath);
request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"overwrite\"\r\n\r\ntrue\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
string test = response.Content.ToString();

*note: I'm using an older version of RestSharp (105.2.3) to be compatible with .Net 4.0 (stuck with it in this case). *注意:我使用的是较旧版本的RestSharp(105.2.3),以便与.Net 4.0兼容(在这种情况下,请保留在它上面)。

Any ideas as to what I'm doing wrong here? 关于我在这里做错什么的任何想法?

Figured it out after sleeping on it, was really simple in the end. 睡上后想通了,最后真的很简单。 All of the extra webkit and multipart stuff that was throwing me for a loop is entirely unnecessary - autogenerated code from Postman is overly complicated it seems. 使我陷入困境的所有多余的Webkit和多部分东西都是完全不必要的-从Postman自动生成的代码似乎过于复杂。

var client = new RestClient(postURL);
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
request.AddFile("upfile", @xmlPath);
request.AddParameter("overwrite", "true", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
string test = response.Content.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM