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

This topic contains 1 reply, has 2 voices, and was last updated by  Robert Thomson 10 months, 2 weeks ago.

  • Author
  • #78679

    Takafu Kenkomayo

    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 –data password=pass –dump-header %OUTPUT_FOLDER%\headers_received_1.txt –output %OUTPUT_FOLDER%\curl_output_1.html –location > %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 –dump-header %OUTPUT_FOLDER%\headers_received_2.txt –output %OUTPUT_FOLDER%\curl_output_2.html >> %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 ("", $secpasswd)

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

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

    $url = ""

    Invoke-WebRequest -Uri "" -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

  • #78782

    Robert Thomson

    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.

You must be logged in to reply to this topic.