Hey everyone,
I am new to Powershell, I am trying to POST a file/data unto a REST API, I created the following code, this works in powershell 6 but doesn’t work in powersehll 5. I am trying to find something to run with powershell 5, need help. Appreciate responses, thanks in advance
$user = ''
$pass = ''
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $pair"
$secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($user, $secpasswd)
$Headers = @{
Authorization = $basicAuthValue
ContentType = 'application/json'
}
$picPath = "C:\Users\ABC\Documents\FILE.json"
$uri = "rest_api"
$form = @{
image1 = Get-Item -Path 'C:\Users\ABC\Documents\FILE.json'
}
$Result=Invoke-RestMethod -Uri $uri -Method Post -ContentType "multipart/form-data" -FORM $form -Headers @{"Authorization"="Basic $encodedCreds"} -Outfile 'C:\Users\ABC\Documents\response.json'
Write-Output $Result.result