Posting a file unto REST API

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

Viewing 1 reply thread
  • Author
    Posts
    • #171226
      Participant
      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: 1154
      Points: 4,012
      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 ?

Viewing 1 reply thread
  • The topic ‘Posting a file unto REST API’ is closed to new replies.