Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Uploading File using HttpClient in c#

I need to write a post method for which I need to upload a file to the server.I converted the file to byte array but still can't do it.The server always returns bad request.Here's the code snippet.

 _httpClient.DefaultRequestHeaders.Authorization = new 
System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _bearerToken);

        HttpContent stringContent = new StringContent("\"" + 
Path.GetFileNameWithoutExtension(kcfFile) + "\"");

        var form = new MultipartFormDataContent();

        byte[] stream = File.ReadAllBytes(kcfFile);


        ByteArrayContent byteContent = new ByteArrayContent(stream);
        form.Add(byteContent, "content");



        HttpContent libContent = new StringContent(selectedLibrary);
        form.Add(libContent, "libraries");
        form.Add(stringContent, "name");

        form.Headers.Add("Bearer", _bearerToken);

        var response = _httpClient.PostAsync(url, form).Result;

The server always returns Bad request.However using postman I can easily Upload.Hence the issue is definitely not the URL.

Thanks in Advance

Comments