title: Post from xamarin to restful API
body: <p>I am trying to make an API call from xamarin, this is my first Full mobile app calling restful API (.NetCore3.1) with a Angular WebApp.
  All other API calls from the xamarin app are working,
  The issue is I need to send a File with an object, file can be .doc .png etc.</p>
  <p>GrantApplication.cs
  API Endpoint</p>
  <pre><code>    public class FileProvider
      {
          public string GA { get; set; }
          public IList&lt;IFormFile&gt; Files { get; set; }
      }
  
  
           [HttpPost]
          [Route(&quot;[action]&quot;)]
          public ActionResult SubmitGrantApplication([FromForm] FileProvider fileProvider)
          {
              try
              {
                  var ga = fileProvider;
                  byte[] fileContent = null;
                  var files = Request.Form.Files.Any() ? Request.Form.Files : new FormFileCollection();
                  var grant = JsonConvert.DeserializeObject&lt;GrantApplication&gt;(ga.GA);
                 ...
              }
          }
  
  </code></pre>
  <p>GrantService.cs
  Call made from the mobile app:
  This call is returning a 400 response with no clear indication why.
  I am trying to figure out why, And my postman skills aren't that great to convert the code to postman.</p>
  <p>The error returned at #5.
  <br/>
  ::&quot;Object reference not set to an instance of an object.&quot;</p>
  <pre><code>public class GrantService
      {
          public static async Task&lt;GrantApplication&gt; SubmitGrant(GrantApplication ga,string file, string FileName, StreamContent FileData)
          {
                  Uri requestUri = new Uri($&quot;{ApiSettings.ApiBaseUrl}/grantapplication/SubmitGrantApplication&quot;);
                  try
                  {
                      var upfilebytes = File.ReadAllBytes(file);
                      //using (HttpClient client = new HttpClient())
                      //{
                      //    //MultipartFormDataContent content = new MultipartFormDataContent();
                      //    //ByteArrayContent baContent = new ByteArrayContent(upfilebytes);
                      //    //StringContent GA = new StringContent(JsonConvert.SerializeObject(ga));
                      //    //content.Add(baContent, &quot;Files&quot;, FileName);
                      //    //content.Add(GA, &quot;GA&quot;);
                      //    //Console.WriteLine(content);
                      //    //var response = await client.PostAsync(requestUri, content);
                      //    //Console.WriteLine(await response.Content.ReadAsStringAsync());
                      //    //return ga;
                      //}
                      using (var formContent = new MultipartFormDataContent())
                      {
                          formContent.Headers.ContentType.MediaType = &quot;multipart/form-data&quot;;
                          // 3. Add the filename C:\\... + fileName is the path your file
                          Stream fileStream = File.OpenRead(file);
                          formContent.Add(new StreamContent(fileStream), FileName, FileName);
                          var payload = JsonConvert.SerializeObject(ga);
                          HttpContent content = new StringContent(payload, Encoding.UTF8, &quot;application/json&quot;);
                          formContent.Add(content, &quot;GA&quot;);
                          using (var client = new HttpClient())
                          {
                              // Bearer Token header if needed
                              client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(&quot;multipart/form-data&quot;));
  
                              try
                              {
                                  // 4.. Execute the MultipartPostMethod
                                  var message = await client.PostAsync(requestUri, formContent);
                                  // 5.a Receive the response
                                  var result = await message.Content.ReadAsStringAsync();
                                  Console.WriteLine(result);
                              }
                              catch (Exception ex)
                              {
                                  // Do what you want if it fails.
                                  throw ex;
                              }
                          }
                      }
                      return ga;
                  }
                  catch(Exception ex)
                  {
                       Console.WriteLine(ex);
                      return ga;
                  }
             
          }
      }
  </code></pre>
  <p>Web System
  Angular API call</p>
  <pre><code>public SubmitGrantApplication(grantApplication){
      grantApplication.memberId = JSON.parse(sessionStorage.getItem(&quot;LoggedInUser&quot;)).memberID;
      grantApplication.ClientID = JSON.parse(sessionStorage.getItem(&quot;LoggedInUser&quot;)).clientID;
      this.headers = new HttpHeaders({ 'Content-Type' :  'multipart/form-data' });
      this.headers = new HttpHeaders({ 'Accept':  'multipart/form-data'});
      let myFormData: FormData = new FormData();
      myFormData.append(&quot;Files&quot;, grantApplication.documentContent);
      myFormData.append(&quot;GA&quot;, JSON.stringify(grantApplication));
      return this.http.post&lt;GrantApplication&gt;(this.url+&quot;SubmitGrantApplication&quot;, myFormData , { headers: this.headers }).pipe(
      ).toPromise();
    }
    
  </code></pre>
  <p>Swagger</p>
  <p><a href="https://i.stack.imgur.com/PLtPY.png" rel="nofollow noreferrer">Swagger Doc</a></p>
  <p>Works
  <a href="https://i.stack.imgur.com/txQRu.png" rel="nofollow noreferrer">Angular API Call</a>
  Works
  <a href="https://i.stack.imgur.com/MUfbT.png" rel="nofollow noreferrer">Payload Angular</a></p>