How to send form with attachment to DIXA in C#

What you will learn

In this post, I am gonna share a piece of code allowing to send a message to the DIXA webform endpoint via C# programmatically. Before we go further, please make sure you've read the official documentation here:

You will find there some useful resources on how to: 

  • Set up a webform endpoint
  • Open a conversation thread using different technologies
  • Handle response

Once we get working endpoint like:

"" and with some basic C# skills - we are ready to bake 🥧 some code and start talking to DIXA!

In the example below, we will send the data from the contact form (with an attachment) including some common fields (name, email, subject, message).

Steps you need to take

To send a request to DIXA programmatically, we need to take the following steps:

  • Prepare HttpClient to make a POST request asynchronously
  • Create & fill MultipartFormDataContent with common fields (name, email, subject, message)
  • Add file attachment
  • Send a POST request to the DIXA webform endpoint
  • Deserialize JSON response
  • Verify if the message was delivered and the conversation was created

Set proper Content-Type

Setting the proper content type before sending the request is a critical part, as official docs say:

"ensure that you are using multipart/form-data type of form"

Common pitfalls:

Got statuscode 'UnsupportedMediaType' from remote server

The request content was malformed:
Content-Type with a multipart media type must have a 'boundary' parameter

Working code sample

public async Task<(bool isOk, string message)> SendFormToDixaEndpoint(
         string name,
         string email,
         string subject,
         string messsage,
         string filePath)
   string message = string.Empty;
   bool isOk = false;

      using (var client = new HttpClient())
         using (var multipartFormDataContent = new MultipartFormDataContent())
            var values = new[]
               new KeyValuePair<string, string>("name", name),
               new KeyValuePair<string, string>("email", email),
               new KeyValuePair<string, string>("subject", subject),
               new KeyValuePair<string, string>("message", messsage)

            foreach (var keyValuePair in values)
               multipartFormDataContent.Add(new StringContent(keyValuePair.Value),

            if (!string.IsNullOrWhiteSpace(filePath))
               multipartFormDataContent.Add(new ByteArrayContent(File.ReadAllBytes(filePath)),

            var requestUri = "";
            var response = await client.PostAsync(requestUri, multipartFormDataContent);

            if (response.IsSuccessStatusCode)
               var jsonContent = await response.Content.ReadAsStringAsync();

               var result = JsonHelper.DeSerialize<DixaFormResponse>(jsonContent);

               if (!string.IsNullOrWhiteSpace(result.Csid))
                  message = "Form was send successfully";
                  isOk = true;
   catch (Exception ex)
      message = "Form was not send";
      isOk = false;

      logger.Error(message, ex);

   return (isOk: isOk, message: message);

Expected response

After a successful request to DIXA, we are expecting a JSON object response with one string field called csid (conversation id).

    "csid": 1234

It's good to create a class to deserialize the JSON response to keep the code nice and clean.

I am not going into deserialization that much - you can use a few popular libraries like Newtonsoft.Json to easily convert JSON string to a strongly typed object. 

public class DixaFormResponse
   public string Csid { get; set; }

Final words

Integration with DIXA can be done very easily and it gives a lot of possibilities.

I hope you find this short post useful and you can adapt it to your needs.


Leave a Comment