C
C#2y ago
Omnissiah

✅ powershell post MultipartContent weird behavior

simply put, i'm making a request and content is serialized with type name instead of real content
Add-Type -assemblyname System.Net.Http

function Create-Multipart-Settings($section_data) {
$multipartContent = [System.Net.Http.MultipartFormDataContent]::new()

$uploadStringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new('form-data')
$uploadStringHeader.Name = "set_params"

$uploadStringContent = [System.Net.Http.StringContent]::new('upload');
$uploadStringContent.Headers.ContentDisposition = $uploadStringHeader;

$multipartContent.Add($uploadStringContent, 'set_params')

$contentStringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new('form-data')
$contentStringHeader.Name = "user_file"

$fileStringContent = [System.Net.Http.StringContent]::new($section_data);
$fileStringContent.Headers.ContentDisposition = $contentStringHeader

$multipartContent.Add($fileStringContent, 'user_file', 'set_params.dat')
return $multipartContent
}

$mc = Create-Multipart-Settings 'data'
$headers = @{}
$headers.Add(...) # authorization stuff
$headers.Add('Content-Type', 'application/x-www-form-urlencoded')
Invoke-WebRequest -Body $mc -Method 'POST' -Uri 'http://...' -headers $headers -TimeoutSec 1800
Add-Type -assemblyname System.Net.Http

function Create-Multipart-Settings($section_data) {
$multipartContent = [System.Net.Http.MultipartFormDataContent]::new()

$uploadStringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new('form-data')
$uploadStringHeader.Name = "set_params"

$uploadStringContent = [System.Net.Http.StringContent]::new('upload');
$uploadStringContent.Headers.ContentDisposition = $uploadStringHeader;

$multipartContent.Add($uploadStringContent, 'set_params')

$contentStringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new('form-data')
$contentStringHeader.Name = "user_file"

$fileStringContent = [System.Net.Http.StringContent]::new($section_data);
$fileStringContent.Headers.ContentDisposition = $contentStringHeader

$multipartContent.Add($fileStringContent, 'user_file', 'set_params.dat')
return $multipartContent
}

$mc = Create-Multipart-Settings 'data'
$headers = @{}
$headers.Add(...) # authorization stuff
$headers.Add('Content-Type', 'application/x-www-form-urlencoded')
Invoke-WebRequest -Body $mc -Method 'POST' -Uri 'http://...' -headers $headers -TimeoutSec 1800
with wireshark i see in the request this is sending that content is serialized as System.Net.Http.StringContent System.Net.Http.StringContent and i've got no clue on how to solve this maybe remove everything and just send the body as string powershell is 5.1 on my machine it works™️, on the one i need to execute the script it doesn't edit apparently it doesn't work on my machine either? im pretty sure it used to work
1 Reply
Omnissiah
OmnissiahOP2y ago
well apparently this works so something went stupidly wrong
$mc = [System.Net.Http.MultipartFormDataContent]::new()
$mc.Add([System.Net.Http.StringContent]::new('file data'), "filedata", 'file.dat')
$mc.Add([System.Net.Http.StringContent]::new('data'), "upload")
invoke-RestMethod -uri 'http://google.com' -method 'POST' -body $mc
$mc = [System.Net.Http.MultipartFormDataContent]::new()
$mc.Add([System.Net.Http.StringContent]::new('file data'), "filedata", 'file.dat')
$mc.Add([System.Net.Http.StringContent]::new('data'), "upload")
invoke-RestMethod -uri 'http://google.com' -method 'POST' -body $mc
ok found something when i create [System.Net.Http.MultipartFormDataContent]::new() in a function it doesn't work when i create on the main scope it works wtf i got it this is the error $mc = [System.Net.Http.MultipartFormDataContent]::new() in some weird way it doesn't actually return anything thanks for the help, myself i never thought powershell was such crap days to figure out this thing, powershell gives no error, no warnings and even then, no idea about how to make this work sometimes object is fine, sometimes not examples and doc around internet don't help this is a nightmare other evidence (in the furnace) when i do new-object multipartformdatacontent and then .Add(stringcontent) it's ok but if i do that in a function, it returns the stringcontent why 😭 maybe i understood you have to use return (,$value) i have no idea why they should do this this makes no sense, why change a universal standard in programming have a particular way to express that a function should return a specific value explained just as a note in an about who tf would ever guess the return keyword is the problem in a function this took me like a day? almost? to figure out like i don't know who came up with this but it's one of the most stupid and unintuitive things i've seen on this planet and stuff gets even crazier but who cares
Want results from more Discord servers?
Add your server