Posting a file unto REST API

Welcome Forums General PowerShell Q&A Posting a file unto REST API

This topic contains 1 reply, has 2 voices, and was last updated by

Senior Moderator
1 month ago.

  • Author
  • #171226

    Topics: 1
    Replies: 0
    Points: -4
    Rank: Member

    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
  • #171250

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,438
    Helping Hand
    Rank: Community Hero

    Never tried "multipart/form-data" type.

    but doesn't work in powersehll 5

    Are you getting any error , or it just does nothing ?

You must be logged in to reply to this topic.