0

My powershell invoke-restmethod keeps giving the error: (415) Unsupported Media Type

This is my script:

add-type -AssemblyName System.Net.Http


    $url = 'https://staging.tiptrack.nl/Tiptrack.Employer.Api/odata/Employers/UploadIncrementalEmployeeImport'
    $accesstoken = $token_tiptrack.access_token

    $filePath = 'C:/afas/Tiptrack_gebruikers.csv'
    $fileName = 'Tiptrack_gebruikers.csv'

    $header = @{
    Authorization="Bearer 1235405sglhfdsgnsg;lsfnsljgflsdjgs'gresgsdfjlaskdf"  
    }

    $content = New-Object System.Net.Http.MultipartFormDataContent
    $fileStream = [System.IO.File]::OpenRead($filePath)
    $fileName = [System.IO.Path]::GetFileName($filePath)
    $fileContent = New-Object System.Net.Http.StreamContent($fileStream)
    $fileContent.Headers.ContentType = New-Object System.Net.Http.Headers.MediaTypeHeaderValue 'text/csv'
    $content.Add($fileContent, 'file', $fileName)


    Invoke-RestMethod -Uri $url -Method Post -Headers $header -Body $content

Hope you can help me.

Martijnsp1991
  • 15
  • 1
  • 5
  • 1
    What version of PowerShell (`$PSVersionTable.PSVersion`)? The ability to use `Invoke-RestMethod` with a `MultipartFormDataContent` instance was only added in PowerShell 6, which is still not the default on most systems. – Jeroen Mostert Jul 06 '20 at 09:27
  • Thnx Jeroen, you solved my problemen :) – Martijnsp1991 Aug 10 '20 at 12:36

1 Answers1

0

What version of PowerShell ($PSVersionTable.PSVersion)? The ability to use Invoke-RestMethod with a MultipartFormDataContent instance was only added in PowerShell 6, which is still not the default on most systems.

Martijnsp1991
  • 15
  • 1
  • 5