title: Sending "complex" model data over UnityWebRequest
body: <p>I am trying to consume some simple .NET API's in my Unity project. I have been able to populate models (classes) on the API server by specifying manually the name of each field. The issue is the content of these models are mainly primitive data types, mainly ints and strings. My model structures are going to require nested models (classes), for example:</p>
  
  <pre><code>public class Echo
  {
      public string text { get; set; }
  }
  
  public class ComplexData
  {
      public List&lt;Echo&gt; echo { get; set; }
      public string description { get; set; }
  }
  </code></pre>
  
  <p>I have the exact same models in Unity, <code>serializable</code> and without the getters and setters as this is what I have seen done in my research.</p>
  
  <p>So this is the code I am using to send this model, which is populated before being sent to this function (and confirmed by a breakpoint).
  Keep in mind I have tried <code>WWWForm</code>, <code>Dictionary</code>, and <code>IMultipartFormSection</code>
  data types to no avail. I have left them in the code simply to demonstrate what I have tried thus far:</p>
  
  <pre><code>IEnumerator sendComplexModel(string url, ComplexData complexData)
  {
      string complex = JsonUtility.ToJson(complexData);
  
      WWWForm form = new WWWForm();
      form.AddField("complexData", complex);
  
      Dictionary&lt;string, string&gt; keyValuePairs = new Dictionary&lt;string, string&gt;();
      keyValuePairs.Add("complexData", complex);
  
      List&lt;IMultipartFormSection&gt; formData = new List&lt;IMultipartFormSection&gt;();
      formData.Add(new MultipartFormDataSection("complexData", complex));
  
      UnityWebRequest uwr = UnityWebRequest.Post(url, formData);
      yield return uwr.SendWebRequest();
  
      if (uwr.isNetworkError)
      {
          Debug.Log("Something went wrong");
      }
      else
      {
          Debug.Log(uwr.downloadHandler.text);
      }
  }
  </code></pre>
  
  <p>You can assume that I have updated the parameters: <code>UnityWebRequest uwr = UnityWebRequest.Post(url, formData);</code> to use the relevant data structures I created above for testing</p>
  
  <p>Every one of the overloaded functions for <code>UnityWebRequest</code> passes null to .NET as seen in this screenshot from Visual Studio:</p>
  
  <p><a href="https://i.stack.imgur.com/7V2lT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7V2lT.png" alt="Visual Studio API"></a></p>
  
  <p>What am I doing wrong? Is something like this even possible with the standard Unity libraries? I would prefer to stick to <code>UnityWebRequest</code> or whatever similar library is built into Unity.</p>
  
  <p>Here is a side by side of the two models (classes), the left being the .NET API, the right being Unity:
  <a href="https://i.stack.imgur.com/oxqZj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oxqZj.png" alt="Side by side of client and api models"></a></p>
  
  <p>Any help would be appreciated, thanks in advance.</p>
  
  <p>====================Update=======================</p>
  
  <p>@LukeBriggs</p>
  
  <p>I have implemented your code as follows, once again sanity check, adding a breakpoint before the web call to double-check I am actually sending a populated model.</p>
  
  <p>This is how I've updated my code:</p>
  
  <pre><code>    string complex = JsonUtility.ToJson(complexData);
      UnityWebRequest uwr = UnityWebRequest.Post(url, complex);
      uwr.SetRequestHeader("Content-Type", "application/json");
  
      yield return uwr.SendWebRequest();
  
      if (uwr.isNetworkError)
      {
          Debug.Log("Something went wrong");
      }
      else
      {
          Debug.Log(uwr.downloadHandler.text);
      }
  </code></pre>
  
  <p>Visual Studio now throughs a null reference exception, while previously it would simply show the model properties/fields are null. Screenshot:</p>
  
  <p><a href="https://i.stack.imgur.com/0QWb2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0QWb2.png" alt="enter image description here"></a></p>