I've been trying to build out some code in PowerShell (PowerShell 6 Core works, the following code didn't work in PowerShell 5),
$Token = ' Token Here ';
$Headers = @{
Method = 'POST'
Uri = ' URL Here '
Headers = @{Authorization = "Bearer $Token" }
}
$bodyHere = @{
roomId = ' ChatSpace ID Here '
text = ' Some Random Text '
files = Get-Item -Path 'c:\test.png'
}
try {
Invoke-RestMethod @Headers -Form $bodyHere
} catch [System.Net.WebException] {
Write-Error $_.Exception.ToString()
throw $_
}
This works well and can upload the file, however I need to also add Content-Type: "image/png"
to my Get-Item
item - is there an easy way to do this?
I've also trying to do it another way by building a multipart form I've seen someone else use on StackOverflow, but I get another issue now where I can't add to the multipart form when I attempt to use Add-Member
or use any other method to append to the form.
$Token = ' Token Here ';
$Headers = @{
Method = 'POST'
Uri = ' URL Here '
Headers = @{Authorization = "Bearer $Token" }
}
$bodyLines = @{
roomId = ' ChatSpace ID Here '
text = ' Random Text here '
}
$FilePath = 'c:\test.png'
$FieldName = 'files'
$ContentType = 'image/png'
$FileStream = [System.IO.FileStream]::New($filePath, [System.IO.FileMode]::Open)
$FileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::New('form-data')
$FileHeader.Name = $FieldName
$FileHeader.FileName = Split-Path -Leaf $FilePath
$FileContent = [System.Net.Http.StreamContent]::New($FileStream)
$FileContent.Headers.ContentDisposition = $FileHeader
$FileContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse($ContentType)
$MultipartContent = [System.Net.Http.MultipartFormDataContent]::New()
$MultipartContent.Add($FileContent)
$MultipartContent.Add($bodyLines) # this doesn't work as I'm building a multipart form
$MultipartContent | Add-Member -Name "headers" -Value $bodyLines -MemberType NoteProperty # I convert this to JSON and use write-host but it doesn't append I've also tried $MultipartContent.headers and it still doesn't append
try {
# Invoke-RestMethod @Headers -Body $MultipartContent
Invoke-WebRequest @Headers -Body $bodyLines
} catch [System.Net.WebException] {
Write-Error $_.Exception.ToString()
throw $_
}
Any help on either how to build a multipart form with both a file upload and other parameters or tacking on content-type to a Get-Item call, would be super appreciated. Just to give you an idea of what the code looks like in Python (much easier):
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
m = MultipartEncoder({'roomId': ' ChatSpace ID Here ',
'text': ' Random Text Here ',
'files': ('example.png', open('example.png', 'rb'),
'image/png')})
r = requests.post(' URL Here ', data=m,
headers={'Authorization': 'Bearer TokenHere',
'Content-Type': m.content_type})
print r.text
I think you want the content of the file instead of the file information. Try something like
files = (Get-Content 'c:\test.png')
in your script. If you just want the path to file you don't need to use Get-Item at all.If you are uploading a .PNG file this may not work if the bytes include control characters that fool the server side parsing of the input.