title: Difficulty sending REST API request with form-data in Powershell using C# static methods
body: <p>I am writing a script that needs to send an API request with some form data. </p>
  
  <p>I am unfortunately unable to use Invoke-RestMethod's -Form option because I need to make this backwards compatible past v6.0.</p>
  
  <p>I created a request in Postman and used their generated code snippet (similar to the strategy found here [<a href="https://get-powershellblog.blogspot.com/2017/09/multipartform-data-support-for-invoke.html][1]" rel="nofollow noreferrer">https://get-powershellblog.blogspot.com/2017/09/multipartform-data-support-for-invoke.html][1]</a>) with hardcoded values and everything works fine. So this works fine:</p>
  
  <pre><code>$apiHost = 'instance.api.com'
  $apiUsername = 'username'
  $apiPassword = 'password123'
  
  $multipartContent = [System.Net.Http.MultipartFormDataContent]::new()
  
  $stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
  $stringHeader.Name = 'deviceId'
  $StringContent = [System.Net.Http.StringContent]::new('123456')
  $StringContent.Headers.ContentDisposition = $stringHeader
  $multipartContent.Add($stringContent)
  
  $stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
  $stringHeader.Name = 'sendEmail'
  $StringContent = [System.Net.Http.StringContent]::new('true')
  $StringContent.Headers.ContentDisposition = $stringHeader
  $multipartContent.Add($stringContent)
  
  $stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
  $stringHeader.Name = 'emailSubject'
  $StringContent = [System.Net.Http.StringContent]::new('example email subject')
  $StringContent.Headers.ContentDisposition = $stringHeader
  $multipartContent.Add($stringContent)
  
  
  $headers = @{
      Authorization = 'Basic ' + 
      [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($apiUsername):$($apiPassword)"))
  }
  
  $invokeRestMethodParams = @{
      Headers = $headers
      Method  = 'PUT'
      URI     = "https://$apiHost/api/version/endpoint/sendmessage"
      Body    = $multipartContent
  }
  
  $response = Invoke-RestMethod @invokeRestMethodParams
  $response | ConvertTo-Json
  </code></pre>
  
  <p>But I get an error from the api that I'm not passing in the correct parameters when I try to pass in my strings as PowerShell variables. I need to do this to loop over the logic and dynamically pass in values provided by the user. This code reaches the API but sends an error that the parameters are missing:</p>
  
  <pre><code>$apiHost = 'instance.api.com'
  $apiUsername = 'username'
  $apiPassword = 'password123'
  
  $multipartContent = [System.Net.Http.MultipartFormDataContent]::new()
  
  $key0 = 'deviceId'
  $val0 = '123456'
  
  $stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
  $stringHeader.Name = $key0
  $StringContent = [System.Net.Http.StringContent]::new($val0)
  $StringContent.Headers.ContentDisposition = $stringHeader
  $multipartContent.Add($stringContent)
  
  $key1 = 'sendEmail'
  $val1 = 'true'
  
  $stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
  $stringHeader.Name = $key1
  $StringContent = [System.Net.Http.StringContent]::new($val1)
  $StringContent.Headers.ContentDisposition = $stringHeader
  $multipartContent.Add($stringContent)
  
  $key2 = 'emailSubject'
  $val2 = 'example email subject'
  
  $stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
  $stringHeader.Name = $key2
  $StringContent = [System.Net.Http.StringContent]::new($val2)
  $StringContent.Headers.ContentDisposition = $stringHeader
  $multipartContent.Add($stringContent)
  
  
  $headers = @{
      Authorization = 'Basic ' + 
      [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($apiUsername):$($apiPassword)"))
  }
  
  $invokeRestMethodParams = @{
      Headers = $headers
      Method  = 'PUT'
      URI     = "https://$apiHost/api/version/endpoint/sendmessage"
      Body    = $multipartContent
  }
  
  $response = Invoke-RestMethod @invokeRestMethodParams
  $response | ConvertTo-Json
  </code></pre>