PDF corrupted when uploading a document with .NET HttpClient

306 Views Asked by At

I am trying to upload a document to this AdobeSign API endpoint

While I have found a way to do it succesfully with the RestSharp RestClient with my below code:

var client = new RestClient("https://api.na2.echosign.com/api/rest/v6/transientDocuments");
            var request = new RestRequest(Method.POST);
            request.AddHeader("Authorization", "Bearer MyIntegratorKey");

            var bytes = Convert.FromBase64String(base64Document);
            var contents = new MemoryStream(bytes);
            request.Files.Add(new FileParameter
            {
                Name = "File",
                Writer = (s) =>
                {
                    var stream = contents;
                    stream.CopyTo(s);
                    stream.Dispose();
                },
                FileName = "Test2.pdf",
                ContentType = null,
                ContentLength = bytes.Length
            });
            IRestResponse response = client.Execute(request);
            Console.WriteLine(response.Content);

I am having issues when I try to use the .NET HttpClient. My below code successfully upload the document (HTTP 201 returned by Adobe) but the document is completely messed up when the signers open it.

Doesn't the .NET HttpClient support file uploads ? is there something wrong with my stream ? Thank you in advance

using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse("Bearer IntegratorKey");
        using (var content = new MultipartFormDataContent())
        {
            content.Add(new StreamContent(new MemoryStream(Convert.FromBase64String(document.EmbeddedContent))), "File", "Test2.pdf");

            using (
                var message =
                await client.PostAsync("https://api.na2.echosign.com/api/rest/v6/transientDocuments", content))
            {
                var input = await message.Content.ReadAsStringAsync();
                Console.WriteLine(input);
            }
        }
    }

enter image description here

0

There are 0 best solutions below