none
Convert mutipart/form-data curl into powershell Invoke-WebRequest RRS feed

  • Question

  • 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-Dis name="file1"',
    ",
    "$FullPathTSVToSend",
    "–$boundary",
    'Content-Dis 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

    • Moved by Bill_Stewart Monday, April 30, 2018 8:47 PM This is not "scripts on demand"
    Friday, September 1, 2017 5:49 PM