Search code examples
httppowershellmultipartform-data

Http multipart/form-data not being


$boundary="---------------------------f35f7ab24fd40e79"
$parameters = "
--$boundary
Content-Disposition: form-data; name=`"name`"
upload.txt
--$boundary
Content-Disposition: form-data; name=`"file`"; filename=`"input.txt`"
Content-Type: text/plain
--$boundary--" 
$SITE="http://httpbin.org/post"
$http_request = New-Object -ComObject Msxml2.XMLHTTP
$http_request.open('POST', $SITE, $false)
$http_request.setRequestHeader("Content-type", "multipart/form-data; boundary=$boundary")
$http_request.setRequestHeader("Connection", "close")
$http_request.send($parameters)
Write-Host $http_request.Status "&"  $http_request.statusText
Write-Host $http_request.ResponseText

What's wrong with the format of this http header that uses multipart/form-data? When you run the above code nothing gets posted, httpbin.org displays nothing in the forms section. I've checked it against other multipart/form-data packets and they're near similar.


Solution

  • You are missing some needed line breaks between the MIME parts, for starters:

    $parameters = "
    --$boundary
    Content-Disposition: form-data; name=`"name`"
    
    upload.txt
    --$boundary
    Content-Disposition: form-data; name=`"file`"; filename=`"input.txt`"
    Content-Type: text/plain
    
    --$boundary--" 
    

    And you are not actually including any content for input.txt in the second MIME part at all. Is that what you really want - to post a blank file?

    I've checked it against other multipart/form-data packets and they're near similar.

    That implies that they are different, so you need to pay attention to those differences.