'I want to upload txt file to NAS device with Synology FileStation API but I got error. Can you help me with this?

I want to send txt file to NAS device via FileStation API and I am coding in C#. When I posted request in Postman, request is successful and file is uploaded. But in C#, I use RestSharp Framework to post request. But I get error 101 or 401 in some situation. In below, I send my code. Thank you for your replies.

var client = new RestClient(url);
var request = new RestRequest("/webapi/entry.cgi", Method.Post);
request.AddHeader("Cookie", "did="+_did+"; id="+_sid);
request.AddParameter("api", "SYNO.FileStation.Upload", ParameterType.RequestBody);
request.AddParameter("version", "3", ParameterType.RequestBody);
request.AddParameter("method", "upload", ParameterType.RequestBody);
request.AddParameter("path", "my_path", ParameterType.RequestBody);
request.AddParameter("create_parents", "False", ParameterType.RequestBody);
request.AddFile("filename", filename, "application/octet-stream");
var response = client.ExecutePostAsync(request).Result;


Solution 1:[1]

The last line of code is what is wrong. Because it is an Async call, you have to await it. However, that means the method this code is in needs to be marked as async. You aren't showing that code, so not sure if this is in the Main method or not. But ultimately, it should look like this:

public static async Task Main(params object[] parameters)
{
    var client = new RestClient(url);
    var request = new RestRequest("/webapi/entry.cgi", Method.Post);
    request.AddHeader("Cookie", "did="+_did+"; id="+_sid);
    request.AddParameter("api", "SYNO.FileStation.Upload", ParameterType.RequestBody);
    request.AddParameter("version", "3", ParameterType.RequestBody);
    request.AddParameter("method", "upload", ParameterType.RequestBody);
    request.AddParameter("path", "my_path", ParameterType.RequestBody);
    request.AddParameter("create_parents", "False", ParameterType.RequestBody);
    request.AddFile("filename", filename, "application/octet-stream");
    var response = await client.ExecutePostAsync(request);
}

Solution 2:[2]

I have encountered the same problem, using Java, the server returns 101. If there is a solution, please let me know. Thank you.

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 Daniel Lorenz
Solution 2 YL W