1

Using Postman the API call works. I used their code snippet feature to get the PowerShell equivalent. But when I try it on PowerShell ISE I get "Empty request body not allowed". Why does it think the body is empty? How can I confirm/look at the contents of $body?

using assembly System.Net.Http
using namespace System.Net.Http

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer token")

$multipartContent = [System.Net.Http.MultipartFormDataContent]::new()

$multipartFile = 'C:\Users\xxx\Desktop\msg.json'
$FileStream = [System.IO.FileStream]::new($multipartFile, [System.IO.FileMode]::Open)
$fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$fileHeader.Name = "ParameterRequest"
$fileContent = [System.Net.Http.StreamContent]::new($FileStream)
$fileContent.Headers.ContentDisposition = $fileHeader
$multipartContent.Add($fileContent)
$FileStream.Flush()
$FileStream.Close()

$multipartFile = 'C:\Users\xxx\Desktop\edm_payload'
$FileStream = [System.IO.FileStream]::new($multipartFile, [System.IO.FileMode]::Open)
$fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$fileHeader.Name = "MessagePayload"
$fileContent = [System.Net.Http.StreamContent]::new($FileStream)
$fileContent.Headers.ContentDisposition = $fileHeader
$multipartContent.Add($fileContent)
$FileStream.Flush()
$FileStream.Close()

$body = $multipartContent

$response = Invoke-RestMethod -Method Post -Uri 'https://APIsite' -Headers $headers -Body $body -ContentType "multipart/form-data"

When I look at the contents of $body this is what I get. How can I see the data in each Content?

PS C:\Users\user> $body

Headers                                 
-------                                 
{[Content-Disposition, System.String[]]}
{[Content-Disposition, System.String[]]}

Added -Verbose to the Invoke-RestMethod and I see

VERBOSE: POST https://APIsite with -1-byte payload

I cannot find what is the meaning of "-1-byte payload". Anybody knows?

  • To answer your question on how you can examine the contents of the $body variable, in Powershell ISE you can set breakpoints on lines by hitting F9. A breakpoint will be set on whatever line the cursor is resting on. Then when you run the script the code will pause execution on that line and you can examine the state of any of your variables at that point in the script. You can do this by hovering over the variable in the editor or even typing the variable in the console and hitting enter. This is called debugging. – Daniel Feb 13 '21 at 04:57
  • Check out this [page](https://devblogs.microsoft.com/scripting/use-the-debugger-in-the-windows-powershell-ise/) for more on how to use the ISE debugger. – Daniel Feb 13 '21 at 04:58

1 Answers1

0

Solved. Part of the problem was that I was using PowerShell v5, the MultipartFormDataContent has been improved since then. Updated to version 7 of PowerShell, made a few tweaks... Success!

$formContent = New-Object -TypeName 'System.Net.Http.MultipartFormDataContent'

$filePath = "C:\Users\xxx\Desktop\msg.json"
$fileStream1 = [System.IO.File]::Open($filePath, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read)
$formContent.Add([System.Net.Http.StreamContent]::new($fileStream1), "ParameterRequest", (Split-Path $filePath -leaf))

$filePath = "C:\Users\xxx\Desktop\edm_payload"
$fileStream2 = [System.IO.File]::Open($filePath, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read)
$formContent.Add([System.Net.Http.StreamContent]::new($fileStream2), "MessagePayload", (Split-Path $filePath -leaf))

Invoke-RestMethod -Method Post -Uri 'https://APIsite' -Headers $requestHeader -Body $formContent -ContentType 'multipart/form-data'

Also - close your streams after the API call