C#C
C#3y ago
deprecatio

✅ 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


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
Was this page helpful?