When I run the following command using cURL.exe to upload a file, I have no issues:
Curl.exe -X POST -H "X-API-TOKEN: token here" -F file=@"E:\program files\curl\filetoupload.csv"
https://url.com/files
I need to run it from a script so I thought I would use PowerShell's Invoke-WebRequest.When I run the following code I get
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At E:\Scripts\Celero\qualtrics.ps1:8 char:13
+ $response = Invoke-WebRequest -Headers $headers -Method Post -OutFile ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-
WebRequest], WebException
+ FullyQualifiedErrorId :
WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
$filepath = "e:\program files\curl\filetoupload.csv"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("X-API-TOKEN", "token here")
$urlAPI = "https://url.com/files"
$response = Invoke-WebRequest -Headers $headers -Method 'Post' -OutFile $filepath -URI $urlAPI
I have tried adding
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12,[Net.SecurityProtocolType]::Tls11
and
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
With no luck. Any ideas?
UPDATE I ended up doing this in PowerShell:
param ($curlExe, $filepath, $headers,$qualtricsAPI,$jobfile)
try{
Copy-Item $jobfile -Destination $filepath
$upload = & $curlExe -X POST -H $headers -s -o -w "%{http_code}" -F file=@$filepath $qualtricsAPI
if ($upload -match "200 - OK"){
Write-Host "Upload successful."
Write-Host "Response: "$upload
Remove-Item $filepath
Write-Host "File removed from $($filepath)."
exit 0
}else{
Write-Host "Upload not successful"
Write-Host "Response: "$upload
exit 2
}
}catch{
"Exception Message: $($_.Exception.Message)"
exit 1
}
I've tested it with our automation tool for file transfers and was successful. I was spinning my wheels on Invoke-WebRequest cmdlet.