How to upload multiple files with restsharp?

Viewed 15634

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"
  }
}
1 Answers
Related