Convert mutipart/form-data curl into powershell Invoke-WebRequest

I am trying to convert two curl commands into powershell using Invoke-WebRequest. I got the first curl command converted and it does the handshake. For the second one I got a 404 error. I have trouble with formating the parameters for --form file1=@%TSV_UPLOAD_FILE% --form format=“XYZ User License Upload”…

Here are the curl commands.

curl --verbose --insecure --cookie-jar %OUTPUT_FOLDER%\cookiejar.txt --cookie I2KBRCK=1 --data login=email@gmail.com --data password=pass --dump-header %OUTPUT_FOLDER%\headers_received_1.txt --output %OUTPUT_FOLDER%\curl_output_1.html --location https://website.org/action/doLogin > %OUTPUT_FOLDER%\curl_verbose_output.txt 2>&1

curl --verbose --insecure --cookie %OUTPUT_FOLDER%\cookiejar.txt --form file1=@%TSV_UPLOAD_FILE% --form format=“XYZ User License Upload” --form email=email@gmail.com --dump-header %OUTPUT_FOLDER%\headers_received_2.txt --output %OUTPUT_FOLDER%\curl_output_2.html https://website.org/action/directSubscriptionUpload >> %OUTPUT_FOLDER%\curl_verbose_output.txt 2>&1

Here is the powershell code:

$FullPathTSVToSend = 'C:\file.tsv'
$outFilePath = 'C:\curl_output_1.html'
$outFilePathVerbose = 'C:\curl_verbose_output.txt'

$secpasswd = ConvertTo-SecureString "pass" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("email@gmail.com", $secpasswd)


$boundary = [System.Guid]::NewGuid().ToString() 
$LF = "`n"

         $bodyLines = (
        "--$boundary",
        'Content-Disposition: form-data; name="file1"',
        '',
        "$FullPathTSVToSend",
        "--$boundary",
        'Content-Disposition: form-data; name="format"',
        '',
        "XYZ User License Upload",
        "--$boundary"
        ) -join $LF

        $url = "https://website.org/action/directSubscriptionUpload" 

Invoke-WebRequest -Uri "https://website.org/action/doLogin" -Credential $mycreds -Verbose -SessionVariable myWebSession -Method Post -OutFile $outFilePath 
Invoke-WebRequest $url `
    -Method Post `
    -ContentType "multipart/form-data; boundary=$boundary"`
    -Body $bodylines `
     -Credential $mycreds `
     -Verbose `
     -WebSession $myWebSession `
     -OutFile $outFilePath

In situations like this I generally fall back to using a packet capture using wire shark. That will allow you to examine the format of the data you send to see if it is correctly formatted.

The network doesn’t lie and you get there quicker than with trial and error.