title: Upload a file to Box.com using Powershell
body: <p>I am working on a series of Powershell scripts for my company to use to transfer data to and from Box.com. The one thing I just can't figure out is uploading files. The Box API requires a multipart POST operation for uploads, and I've seen a few answers here on SO indicating that I should be able to do that in Powershell (such as <a href="https://stackoverflow.com/questions/22491129/how-to-send-multipart-form-data-with-powershell-invoke-restmethod">this one</a>). But I can't seem to get it working.</p>
  
  <p>Here's the code I have right now:</p>
  
  <pre><code>Function Post-File {
      Param(
              [Parameter(Mandatory=$True,Position=1)]
              [string]$SourcePath,
              [Parameter(Mandatory=$False,Position=2)]            
              [string]$FolderId = ############
      )
  
      #Variables for building URIs
      $baseUrl = "https://upload.box.com/api/2.0/files/content"
  
      #Set Authorization for API requests
      $headers = @{}
      $AccessToken = Refresh-Tokens #A reference to another function that definitely works
      $headers.Add("Authorization", "Bearer $AccessToken")
  
      #Set POST content
      $body = @{}
      $body.Add("filename",  [IO.File]::ReadAllBytes($SourcePath))
      $body.Add("parent_id", $FolderId)
  
      #Upload the file
      Invoke-RestMethod -Uri $baseUrl -Method Post -Headers $headers -ContentType "multipart/form-data" -Body $body
  }
  </code></pre>
  
  <p>Here's the response I get back:</p>
  
  <pre><code>{
   "type":"error",
   "status":400,
   "code":"invalid_request_parameters",
   "help_url":"http://developers.box.com/docs/#errors",
   "message":"Invalid input parameters in request",
   "request_id":"1764475572534bcddfe04b7"
  }
  </code></pre>
  
  <p>I've also tried a couple of other permutations that aren't working. I've tried using the <code>-InFile</code> switch in <code>Invoke-RestMethod</code> instead of <code>-Body</code>. I've also tried using <code>Get-Content -Raw</code> in place of <code>[IO.File]::ReadAllBytes</code>. Both of those return a more generic error: <code>The server encountered an internal error or misconfiguration and was unable to complete your request.</code>. </p>
  
  <p>I'm pretty sure this has something to do with my <code>filename</code> parameter not being correct, but I'm not sure how to fix it. According to the <a href="https://developers.box.com/docs/#files-upload-a-file" rel="nofollow noreferrer">Box API</a>, here's what it should look like in curl. Can someone help me properly translate this for Powershell?</p>
  
  <pre><code>https://upload.box.com/api/2.0/files/content \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -F filename=@FILE_NAME \
  -F parent_id=PARENT_FOLDER_ID
  </code></pre>