system.Collections.Hashtable does not exist when calling invoke restmethod for csv upload

$uri = “**************”

$body = @{
grant_type = **************
client_id = **************
client_secret = **************
username = **************
password = **************
}

$resp = Invoke-RestMethod -Method POST -Uri $uri -Body $body

$token = $resp.access_token

$boundary = [System.Guid]::NewGuid().ToString();

$filename = “C:\Users**\TestUpload.csv”

$headers = New-Object “System.Collections.Generic.Dictionary[[String],[String]]”
$headers.Add(‘Authorization’, “Bearer $token”)
$headers.Add(‘ContentLength’,8616)

$headers

$form = @{
file = $filename
minorEdit = “true”
}

Invoke-RestMethod ‘https://./.*********’ -Method POST -infile $form -Header $headers -ContentType ‘multipart/form-data; boundary="$boundary"’

token is processed fine however I get the following

Invoke-RestMethod : Cannot find path ‘C:\Users*******\System.Collections.Hashtable’ because it does not exist.

any help would be appreciated

Hi, welcome to the forum :wave:

Firstly, when posting code in the forum, please can you use the preformatted text </> button. It really helps us with readability, and copying and pasting your code (we don’t have to faff about replacing curly quote marks to get things working). If you can’t see the </> in your toolbar, you will find it under the gear icon.

How to format code on PowerShell.org

The problem you’re having is with the InFile parameter. If you check the documentation for Invoke-RestMethod you can see that it’s expecting an object of type System.String and you’re trying to pass it a hashtable (your variable $form).

PS E:\Temp> Get-Help Invoke-RestMethod -Parameter InFile

-InFile <System.String>
    Gets the content of the web request from a file.

    Enter a path and file name. If you omit the path, the default is the current location.

    Required?                    false
    Position?                    named
    Default value                None
    Accept pipeline input?       False
    Accept wildcard characters?  false
1 Like

Hi thanks for your response - noted will use <> for scripts going forward.

Unfortunately I’m getting the following now when hardcoding the file

Invoke-RestMethod : {“message”:“Invalid file”}

Invoke-RestMethod 'https://******.*******.********/******/******' -Method POST -inFile "C:\Users\**********\*********\*****\TestUpload.csv" -Header $headers -ContentType 'multipart/form-data; boundary=`"$boundary`"'

So as I’m using “multipart/form-data” as the content type (this type works via postman and swagger)
Is it ok to simply use a .csv file in this request or do i need to do additional work with any type of conversions etc…