Invoke-WebRequest - How to use a nested collection in the Form field?

  • 14 July 2021
  • 0 replies

I need to send a request to the server and everything goes well until I set Custom_Fields. Using the -Body parameter, the nested custom_fields collection is sent to the site, but with -Body I can't send the file in the attachments[] section. I found out that the -Form parameter does not support nested collections. Has anyone encountered this and how can I get around it?

Here is my script:

$APIkey = "MyAPI"
$URL = "URL"

$Headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$APIkey"))}

$ServiceRequest = @{
"email" = "email"
"quantity" = 1
"attachments[]" = Get-Item -Path "C:\CSV-table.csv"
"custom_fields" = @{
"total" = 99
"detected" = "true"
"details" = "Service Request Details:"

$NewServiceRequest = Invoke-WebRequest -Uri $URL -Method Post -Headers $Headers -Form $ServiceRequest

0 replies

Join the Community or User Group to Participate in this Discussion