8

I am trying to convert these two curl commands. I am just unsure on outputing the verbose. And if my cookie needs I2KBRCK=1. And how to do a header dump.

%CURL_FOLDER%\curl --verbose --insecure --cookie-jar %OUTPUT_FOLDER%\cookiejar.txt --cookie I2KBRCK=1 --data [email protected] --data password=pass --dump-header %OUTPUT_FOLDER%\headers_received_1.txt --output %OUTPUT_FOLDER%\curl_output_1.html --location https://website.com/action/doLogin  > %OUTPUT_FOLDER%\curl_verbose_output.txt 2>&1
%CURL_FOLDER%\curl --verbose --insecure --cookie %OUTPUT_FOLDER%\cookiejar.txt --form file1=@%TSV_UPLOAD_FILE% --form format="XYZ User License Upload" --form email=email.org --dump-header %OUTPUT_FOLDER%\headers_received_2.txt --output %OUTPUT_FOLDER%\curl_output_2.html https://website.com/something >> %OUTPUT_FOLDER%\curl_verbose_output.txt 2>&1

I converted the curl commands into this powershell.

$outFilePath = 'C:\Users\blah\Desktop\curl_output_1.html'
$outFilePathVerbose = 'C:\Users\blah\Desktop\curl_verbose_output.txt'

$secpasswd = ConvertTo-SecureString "password" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("[email protected]", $secpasswd)

Invoke-WebRequest -Uri "https://website.com/doLogin" -Credential $mycreds -Verbose -SessionVariable myWebSession -Method Post -OutFile $outFilePath 
Invoke-WebRequest -InFile $someFile -Uri "https://website.com/something" -Credential $mycreds -Verbose -WebSession $myWebSession -Method Post -OutFile $outFilePath

I tried to convert the second curl command into powershell another way and got a 404 error instead of a 500 error...

$body = @"
format = "XYZUser License Upload"
file1 = $FullPathTSVToSend
"@
$gist =  Invoke-WebRequest -Body $body   -Uri "https://website.com/action/directSubscriptionUpload"  -Credential $mycreds -Verbose -WebSession $myWebSession -OutFile $outFilePath -Method Post   -ContentType "multipart/form-data" 

I edited with powershell with the new code that you suggested...

$content = Get-Content  $FullPathTSVToSend
$body = @{ 'format' = "XYZUser License Upload"; 'file1' = $( $content); 'email' ="[email protected]"  }
Invoke-WebRequest -Uri "https://website.com/doLogin" -Credential $mycreds -Verbose -SessionVariable myWebSession -Method Post -OutFile $outFilePath 
Invoke-WebRequest -Body $body -Uri "https://website.com/something" -Credential $mycreds -Verbose  -OutFile $outFilePath2 -Method Post -ContentType "multipart/form-data" -WebSession $myWebSession 

However, I am still getting the 404 error for the second Invoke-WebRequest. I think maybe there is something else I need to pass from the first Invoke-WebRequest command. But the myWebSession should have that cookie I2KBRCK=1 from the first curl command.

10
  • So what's your goal here exactly? Commented Aug 31, 2017 at 20:24
  • I am having trouble converting this part of the curl command: file1=@%TSV_UPLOAD_FILE% --form format="XYZ User License Upload" into powershell Invoke-WebRequest Commented Aug 31, 2017 at 20:39
  • Windows and PowerShell versions? Commented Aug 31, 2017 at 20:41
  • powershell 5.1 and windows 10 Commented Aug 31, 2017 at 20:43
  • What is in your -InFile? Commented Aug 31, 2017 at 20:46

1 Answer 1

4
+50

You need to put the form parameters into a hash and make sure you have the content of the file, not the file name, in the request.

Try:

$body = @{ format = "XYZUser License Upload"; file1 = $(gc $FullPathTSVToSend) }
Invoke-WebRequest -Body $body -Uri "https://website.com/action/directSubscriptionUpload" -Credential $mycreds -Verbose -WebSession $myWebSession -OutFile $outFilePath -Method Post -ContentType "multipart/form-data"
Sign up to request clarification or add additional context in comments.

1 Comment

I am still getting a 404 error message. I edited my question with the new code I am using...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.