title: How to do a webrequest with JSON Body containing arrays or lists?
body: <h2>The Situation</h2>
  
  <p>I need to interact with a REST API which requires JSON post data.
  So I started working with something like this:</p>
  
  <h2>Simple working example</h2>
  
  <pre><code>$ReqURI = 'http://httpbin.org/post'
  Invoke-WebRequest -Method Post -Uri $ReqURI -Body @{
      'api.token' = "api.token"
      'action' = 'create item'
  } -Verbose| fl *
  </code></pre>
  
  <p>So I tested it with httpbin.org:</p>
  
  <p><a href="https://i.stack.imgur.com/yeFB1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yeFB1.png" alt="Working"></a></p>
  
  <h2>Issue</h2>
  
  <p>But If you need to use a list or array in the body part like this example:</p>
  
  <pre><code>$ReqURI = 'http://httpbin.org/post'
  $Response = Invoke-RestMethod -Method Post -Uri $ReqURI -Body @{
      'api.token' = "api.token"
      'names' = @('rJENK', 'rFOOBAR')
  } -Verbose| fl *
  $Response
  </code></pre>
  
  <p>... you get something like a converting error:</p>
  
  <p><a href="https://i.stack.imgur.com/8pZQ8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8pZQ8.png" alt="Converting Issue"></a></p>
  
  <p>So I thought I could convert the body myself into a JSON string and using the <code>-Depth</code> parameter from <code>ConvertTo-JSON</code>. In addition to that I tried how it looks like if I convert the hashtable into an object first.</p>
  
  <p>But both tries return the same and even worse result:</p>
  
  <p><a href="https://i.stack.imgur.com/SCXu9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SCXu9.png" alt="not working"></a></p>
  
  <p>So finally I switched to <code>Invoke-WebRequest</code>. But the results here are the same.</p>
  
  <p>My reference is a working api call with the JSON string:</p>
  
  <pre><code>"api.token" : "fooobar",
  "names": [
      "rJENK",
      "rFOOBAR"
    ]
  </code></pre>
  
  <h3>Personal Solution</h3>
  
  <p>I figured out a workaround. It seems like the api I'm working with can't handle requests containing nested elements or array created by PowerShell.</p>
  
  <p>Non working example:</p>
  
  <pre><code>$ReqURI = 'http://httpbin.org/post'
  $Response = Invoke-RestMethod -Method Post -Uri $ReqURI -Body @{
      'api.token' = "api.token"
      'names' = @('rJENK', 'rFOOBAR')
  } -Verbose| fl *
  $Response
  </code></pre>
  
  <p>Instead I had to fake an array with indexes. That's the workaround:</p>
  
  <pre><code>$ReqURI = 'http://httpbin.org/post'
  $Response = Invoke-RestMethod -Method Post -Uri $ReqURI -Body @{
      'api.token' = "api.token"
      'names[0]' = 'rJENK'
      'names[1]' = 'rFOOBAR'
  } -Verbose| fl *
  $Response
  </code></pre>