'Synology File Station API C#
I encounter a trouble with the file station API using RestSharp I would like to upload a file, I follow the document describe in the official API doc ( here )
There is my code :
private const string url_auth = "/webapi/entry.cgi";
public Upload()
{
byte[] data = File.ReadAllBytes(@"/Users/XXXXXX/Downloads/file.csv");
var client = new RestClient(SynSettings.DSM);
var request = new RestRequest(url_auth);
request.AlwaysMultipartFormData = true;
request.Method = Method.POST;
request.AddHeader("Content-Type", $"multipart/form-data; boundary={Guid.NewGuid()}");
request.AddHeader("Content-Length", $"{data.Length}");
request.AddQueryParameter("api", "SYNO.FileStation.Upload");
request.AddQueryParameter("method", "upload");
request.AddQueryParameter("version", "2");
request.AddQueryParameter("_sid", $"{SynSettings.Sid}");
//request.("", $"{SynSettings.Sid}");
request.AddParameter("api", "SYNO.FileStation.Upload", ParameterType.GetOrPost);
request.AddParameter("version", "2", ParameterType.GetOrPost);
request.AddParameter("method", "upload", ParameterType.GetOrPost);
request.AddParameter("path", "/ActiveBackupforBusiness", ParameterType.GetOrPost);
request.AddParameter("create_parents", "false", ParameterType.GetOrPost);
request.AddFileBytes("file", data, "file.csv", "application/octet-stream");
Console.WriteLine(client.BuildUri(request));
var queryResult = client.Execute(request);
Console.WriteLine(queryResult.Content);
}
Thanks for your help
Update 1
The first step, I call the API to get the SID, after this I save it to use it every time with the parameter "_sid"
When I try to upload a file, the server API respond with : {"error":{"code":401},"success":false}
I don't understand what's wrong with my code
Solution 1:[1]
I used it and it works for me. I am using RestSharp 106.15.0, though it isn't working for newer versions.
var path = @"C:\test.txt";
var file = new FileInfo(path);
var request = new RestRequest("", Method.POST);
request.AddParameter("_sid", _session.Sid, ParameterType.QueryString);
request.AddParameter("api", "SYNO.FileStation.Upload", ParameterType.QueryString);
request.AddParameter("version", "2", ParameterType.QueryString);
request.AddParameter("method", "upload", ParameterType.QueryString);
request.AddParameter("path", destinationFilePath);
request.AddParameter("overwrite", overwrite);
request.AddFile(file.Name, file.FullName);
var client = new RestClient("https://YOUR_HOST/webapi/entry.cgi");
var response = client.ExecuteAsync(request).Result;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jeremy Caney |