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

Welcome Forums General PowerShell Q&A Convert mutipart/form-data curl into powershell Invoke-WebRequest

This topic contains 1 reply, has 2 voices, and was last updated by

1 year, 8 months ago.

  • Author
  • #78679

    Topics: 1
    Replies: 0
    Points: 0
    Rank: Member

    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

    Topics: 1
    Replies: 9
    Points: 22
    Rank: Member

    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.

The topic ‘Convert mutipart/form-data curl into powershell Invoke-WebRequest’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort