Demo 2: C#

An example of how to call the upload document endpoint using C#


namespace UploadDocument
{
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Net.Http;
    using System.Threading.Tasks;
    using Newtonsoft.Json;

    public class DocumentUploadResponse
    {
        public long BytesUploaded { get; set; }

        public string Reference { get; set; }

        public bool DocumentTypeValidationSucceeded { get; set; }

        public Guid Id { get; set; }

        public string ResolvedDocumentType { get; set; }
    }

    public class Program
    {
        static async Task Main(string[] args)
        {
            var client = new HttpClient();

            var content = new MultipartFormDataContent();
            
            var image = Image.FromFile("C://your-image-file");

            using (var memStream = new MemoryStream())
            {
                image.Save(memStream, ImageFormat.Png);

                content.Add(new ByteArrayContent(memStream.ToArray()), "document", "document.png");
            }

            content.Add(new StringContent("My-Reference-123"), "reference");

            var response = await client.SendAsync(new HttpRequestMessage
            {
                Method = HttpMethod.Post,
                Headers = { { "Authorization", "Basic " } },
                RequestUri = new Uri("https://api.w2globaldata.com/documentupload?api-version=1.0"),
                Content = content
            });

            if (!response.IsSuccessStatusCode)
            {
                // Handle failed requests
            }

            var documentResponse = JsonConvert.DeserializeObject(
                await response.Content.ReadAsStringAsync());

            Console.WriteLine($"Uploaded document... id: {documentResponse.Id}");
        }
    }
}