powershell - 将 curl 转换为 PowerShell Invoke-WebRequest

标签 powershell curl header session-cookies

我正在尝试转换这两个 curl命令。我只是不确定输出详细信息。如果我的 cookie 需要 I2KBRCK=1。以及如何进行标题转储。

%CURL_FOLDER%\curl --verbose --insecure --cookie-jar %OUTPUT_FOLDER%\cookiejar.txt --cookie I2KBRCK=1 --data user@web.org --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

我将 curl 命令转换为这个 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 ("user@web.org", $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

我试图以另一种方式将第二个 curl 命令转换为 powershell 并得到 404 错误而不是 500 错误...
$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" 

我使用您建议的新代码使用 powershell 进行了编辑...
$content = Get-Content  $FullPathTSVToSend
$body = @{ 'format' = "XYZUser License Upload"; 'file1' = $( $content); 'email' ="user@web.org"  }
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 

但是,我仍然收到第二个 Invoke-WebRequest 的 404 错误。我想也许我需要从第一个 Invoke-WebRequest 命令传递其他东西。但是 myWebSession 应该有来自第一个 curl 命令的 cookie I2KBRCK=1 。

最佳答案

您需要将表单参数放入散列中,并确保在请求中包含文件的内容,而不是文件名。

尝试:

$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"

关于powershell - 将 curl 转换为 PowerShell Invoke-WebRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45869012/

相关文章:

azure - 尝试在 Visual Studio Code 中打开 Azure Cloud Shell 时出现 500 错误

c - 有没有办法在 .c 文件而不是 .h 文件中定义 C 内联函数?

forms - 如何在表单中添加 curl `-H` 参数?

php - 哪个 PHP 函数最适合检索 Web 服务器数据

PHP cURL 调用返回错误 56 和 NSS 错误 -12195

c++ - 在 C++ 中找不到枚举头文件

security - 将 X-Forwarded-For 信任为 "identify"访问者

powershell, send key() 和 appactivate() 奇怪

unicode - 将 Unicode 字符打印到 PowerShell 提示符

.net - 使用 PowerShell/C# 从 cmd 批处理保存文件对话框