Problem with API file upload in C#

Hi there,

I’m having problems with REST API usage in C# trying to upload a file.
Everything works fine (token request, getting upload link) up to the point where I try to send the file using the upload-link which constantly returns ‘Bad Request’ with status code 400.
I guess it is some problem with the multipart data, but I am out of ideas and already tried many things.
Would be really great if someone had an idea…

This is the sample code (.Net 6.0):

using System.Net.Http.Headers;
using System.Reflection;
using System.Text;
using System.Text.Json;

internal class Program
    private const string url = "https://<url>";
    private const string username = "<username>";
    private const string password = "<password>";

    private static async Task Main(string[] args)
        string? Token;
        string? RepoId;
        using (var httpClient = new HttpClient())
            httpClient.BaseAddress = new Uri(url + "/api2/auth-token/");
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url + "/api2/auth-token/");
            var parameters = new Dictionary<string, string> { { "username", username }, { "password", password } };
            request.Content = new FormUrlEncodedContent(parameters);
            var response = await httpClient.SendAsync(request);
            var jsonString = await response.Content.ReadAsStringAsync();
            Token = JsonDocument.Parse(jsonString).RootElement.GetProperty("token").GetString();

            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", parameter: Token);
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            response = await httpClient.GetAsync(url+"/api2/repos/");
            jsonString = await response.Content.ReadAsStringAsync();
            RepoId = JsonDocument.Parse(jsonString).RootElement[0].GetProperty("id").GetString();

            response = await httpClient.GetAsync(requestUri: url+"/api2/repos/" + RepoId + "/upload-link/");
            var responseBody = await response.Content.ReadAsStringAsync();
            var uploadLink = responseBody.Replace("\"", "");

            var multipartContent = new MultipartFormDataContent();
            multipartContent.Add(new ByteArrayContent(File.ReadAllBytes("Test.txt")), "file", Path.GetFileName("Test.txt"));
            multipartContent.Add(new StringContent(""), name: "parent_dir");
            multipartContent.Add(new StringContent("1"), name: "replace");

            response = await httpClient.PostAsync(uploadLink, multipartContent);
            // results in 'Bad Request'

no developers around? I’m really lost… :joy: