Upload Binary file with HttpClient using octet stream

Viewed 1696

I am trying to upload a file using an httpClient as an octet-stream

  1. Following documentation is provided
  2. HTTP Verb => POST
  3. Content–Type => application/octet-stream
  4. Accept => application/json
  5. Authorization => Bearer {access token}
  6. x-file-name => Test file.pdf
  7. x-convert-document (optional) => true
  8. x-source => the source (can be any string)
  9. Request Body => file (binary Stream)

Side note : in my code below the Accept and Bearer headers and their values are already present in the Client I am fetching with the "await GetRestClientAsync()"

I expect a response back, but for now I am getting nothing back the application just stops, what am I doing wrong here?

Ps: Other Posts, request towards this api work fine with the same client I use here, so it's most likely that I am messing something up with the MultiPartFormDataContent

 protected async Task<TResponse> ExecutePostAsBinaryCustomHeadersAsync<TResponse>(Uri apiUri, Stream fileStream, string fileName ,bool returnDefaultObjectForInternalServerError = false)
    {
        using (var formData = new MultipartFormDataContent())
        {
            formData.Add(new StringContent(fileName), "x-file-name");
            formData.Add(new StringContent("true"), "x-convert-document");
            formData.Add(new StringContent("Agion"), "x-source");
            formData.Add(new StringContent("application/octet-stream"), "Content-Type");

            using (var client = await GetRestClientAsync())
            {
                StreamContent content = new StreamContent(fileStream);
                formData.Add(content, "file", fileName);

                using (var response = await client.PostAsync(apiUri, formData))
                {

                    return await DeserializeResponseAsync<TResponse>(response, returnDefaultObjectForInternalServerError); 
                }

            }
        }
    }
0 Answers
Related