title: 400 Bad Request when trying to send api request with IFormFile in request object
body: <p>I am having a hard time figuring out how to send an IFormFile object part of the request. It is an API call to upload an image. I have found a few resources and have tried each suggestion but I always get a 400 Bad Request response when I try and hit the API. Both the API and client are ASP.NET Core 2.1</p>
  
  <p><strong>Call to the API</strong></p>
  
  <pre><code>public async Task&lt;ApiResponse&lt;ImageDto&gt;&gt; AddImageToWebsite(AddImageToWebsiteRequest request)
  {
        try
        {
          HttpClient client = new HttpClient();
          var url = $"{_apiInfo.Url}/portal/AddImageToWebsite";
          byte[] data;
          using (var br = new BinaryReader(request.Image.OpenReadStream()))
          {
            data = br.ReadBytes((int) request.Image.OpenReadStream().Length);
          }
  
          var bytes = new ByteArrayContent(data);
          MultipartFormDataContent multiContent = new MultipartFormDataContent();    
  
          multiContent.Add(bytes, "file", request.Image.FileName);
          multiContent.Add(new StringContent(request.WebsiteId.ToString()), "WebsiteId");
          multiContent.Add(new StringContent(request.AltText), "AltText");
  
          // BREAKS AFTER THIS POST CALL
          var apiResponse = await client.PostAsync(url, multiContent);
  
          // DESERIALIZE RESPONSE TO RESPONSE OBJECT HERE
  
        }
        catch (Exception ex)
        {
          Log.Error(ex, "Error calling api");
          return ApiResponse.InternalError&lt;ImageDto&gt;(ex.Message);
        }
  }
  </code></pre>
  
  <p><strong>AddImageToWebsiteRequest</strong></p>
  
  <pre><code>public class AddImageToWebsiteRequest
  {
      public int WebsiteId { get; set; }
      public IFormFile Image { get; set; }
      public string AltText { get; set; }
  }
  </code></pre>
  
  <p><strong>API CALL</strong></p>
  
  <pre><code>[HttpPost]
  [Route("AddImageToWebsite")]
  public async Task&lt;JsonResult&gt; AddImageToWebsite(AddImageToWebsiteRequest request)
  {
    return await this.HandleRequest(async () =&gt;
    {
      var website = _dataAccess.GetWebsite(request.WebsiteId);
  
      if (website == default(Website))
      {
        return ApiResponse.NotFound&lt;ImageDto&gt;("Website not found");
      }
  
      // UPLOAD IMAGE CODE HERE
    }
  }
  </code></pre>
  
  <p>It does not even hit the API call. I also tried posting it as follows, and it worked as long as I did not have an image in the serialized object.</p>
  
  <p><strong>Another Attempt</strong> </p>
  
  <pre><code>var stringContent = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
  var apiResponse = await client.PostAsync(url, stringContent);
  
  // DESERIALIZE RESPONSE TO RESPONSE OBJECT HERE
  </code></pre>
  
  <p>I have tried so many different recommendations online and none seem to work.</p>