问题描述
我想将文件上传到此api https://support.crowdin.com/如何创建一个名为文件
的参数并添加多个文件使用RestSharp?
我写了这段代码到目前为止,但它不起作用,RestSharp似乎没有按预期上传文件。
$ b
var addUrl = new Uri($https://api.crowdin.com/api/project/{projectIdentifier}/add-file?key= {projectKey}&安培; JSON =);
var restClient = new RestSharp.RestClient(https://api.crowdin.com);
var request = new RestSharp.RestRequest($api / project / {projectIdentifier} / add-file,RestSharp.Method.POST);
request.AlwaysMultipartFormData = true;
request.AddQueryParameter(key,projectKey);
request.AddQueryParameter(json,);
var files = new Dictionary< string,byte []>
{
{testfile,File.ReadAllBytes(fileName)}
};
request.AddParameter(files,files,RestSharp.ParameterType.RequestBody);
var restResponse = restClient.Execute(request);
这给了我
{
success:false,
error:{
code:4,
message:
}
@SirRufo在评论中提到了这个解决方案,但并没有把它作为一个解决方案,所以我将在这里解释它。
http POST方法实际上没有阵列。
在字段名中使用方括号只是一个约定。
这个例子的代码有效:
var restClient = new RestSharp.RestClient(https://api.crowdin.com);
var request = new RestSharp.RestRequest($api / project / {projectIdentifier} / add-file,RestSharp.Method.POST);
request.AlwaysMultipartFormData = true;
request.AddHeader(Content-Type,multipart / form-data);
request.AddQueryParameter(key,projectKey);
request.AddQueryParameter(json,);
request.AddFile(files [testfile1.pot],fileName);
request.AddFile(files [testfile2.pot],fileName);
//只是执行(...)缺少...
不需要嵌套自定义参数或任何复杂的东西。
使用这种特殊名称格式添加文件是所需要的。
我的错误是认为文件[filenamehere。 txt]
部分意味着比实际需要更复杂的POST正文。
I want to upload files to this api https://support.crowdin.com/api/add-file/
How can I create a parameter named files
and add multiple files to it using RestSharp?
I wrote this code so far but it doesn't work, RestSharp does not seem to upload the file as intended.
var addUrl = new Uri($"https://api.crowdin.com/api/project/{projectIdentifier}/add-file?key={projectKey}&json=");
var restClient = new RestSharp.RestClient("https://api.crowdin.com");
var request = new RestSharp.RestRequest($"api/project/{projectIdentifier}/add-file", RestSharp.Method.POST);
request.AlwaysMultipartFormData = true;
request.AddQueryParameter("key", projectKey);
request.AddQueryParameter("json", "");
var files = new Dictionary<string, byte[]>
{
{ "testfile", File.ReadAllBytes(fileName) }
};
request.AddParameter("files", files, RestSharp.ParameterType.RequestBody);
var restResponse = restClient.Execute(request);
This gives me
{
"success":false,
"error":{
"code":4,
"message":"No files specified in request"
}
}
@SirRufo mentioned the solution in the comments but didn't post it as a soltion, so I'll explain it here.
The http POST method actually has no concept of arrays.Instead having square brackets in the field names is just a convention.
This example code works:
var restClient = new RestSharp.RestClient("https://api.crowdin.com");
var request = new RestSharp.RestRequest($"api/project/{projectIdentifier}/add-file", RestSharp.Method.POST);
request.AlwaysMultipartFormData = true;
request.AddHeader("Content-Type", "multipart/form-data");
request.AddQueryParameter("key", projectKey);
request.AddQueryParameter("json", "");
request.AddFile("files[testfile1.pot]", fileName);
request.AddFile("files[testfile2.pot]", fileName);
// Just Execute(...) is missing ...
No need to nest custom parameters or anything complicated like that.Adding the files with this "special" name format is all it takes.
My mistake was thinking that the files[filenamehere.txt]
part implied a more complex POST body than it really needed.
这篇关于如何用restsharp上传多个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!