© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
10 replies
without answer

✅ 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
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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Weird ASP0016 behavior
C#CC# / help
4mo ago
Weird ToBase64String behavior
C#CC# / help
4y ago
Powershell
C#CC# / help
2y ago
✅ Caesar Cipher Code with Weird Behavior
C#CC# / help
3y ago