Add_file_to_column mutation (400) error in powershell

I’m running into a wall with the below code. This code was generated by Postman from a successful request but when run returns :

Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At line:1 char:13

  • $response = Invoke-RestMethod ‘https://api.monday.com/v2/file’ -Metho …
  •         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    • FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Any help pointing out any issues in this code would be appreciated.

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

$multipartContent = [System.Net.Http.MultipartFormDataContent]::new()
$stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$stringHeader.Name = "query"
$stringContent = [System.Net.Http.StringContent]::new("mutation add_file(`$file: File!) {add_file_to_column (item_id: 5209649929, column_id:`"files`" file: `$file) {id}}`n")
$stringContent.Headers.ContentDisposition = $stringHeader
$multipartContent.Add($stringContent)

$stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$stringHeader.Name = "map"
$stringContent = [System.Net.Http.StringContent]::new("{`"image`":`"variables.file`"}`n")
$stringContent.Headers.ContentDisposition = $stringHeader
$multipartContent.Add($stringContent)

$multipartFile = 'C:\Users\MyFile.xlsx'
$FileStream = [System.IO.FileStream]::new($multipartFile, [System.IO.FileMode]::Open)
$fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$fileHeader.Name = "image"
$fileHeader.FileName = "C:\Users\MyFile.xlsx"
$fileContent = [System.Net.Http.StreamContent]::new($FileStream)
$fileContent.Headers.ContentDisposition = $fileHeader
$multipartContent.Add($fileContent)

$body = $multipartContent

$response = Invoke-RestMethod 'https://api.monday.com/v2/file' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json
$FileStream.Dispose()

I found a savior on Reddit. Here you’ll find a block of powershell code that works to upload files to an item:
https://www.reddit.com/r/PowerShell/comments/16ug5zz/comment/k2kyhfp/

Hi @mcdemon and welcome to the community!

I am glad you found a solution for this!

Let us know if you need any help in the future :grin:

Cheers,
Matias

1 Like