4

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.

user2073183
  • 141
  • 1
  • 3
  • 13
  • 1
    We had a similar problem. In the end, it was a network firewall issue (Palo Alto was killing the connection in transit). I still can't explain why cURL and wget worked and iwr did not, obviously the protocols are the same. We opened port 443 to the server and it worked. We also used https://www.nartac.com/Products/IISCrypto/ to turn on the protocols on the server. I don't believe in magic, but this one came close. We couldn't find anything on the Internet other than the general suggestion of adding the protocols to the script that you mentioned in the question. I hope that this helps. – Michael Erpenbeck Nov 13 '20 at 05:38
  • Port 443 is open on the server. Our security team doesn't want 1.1 and 1.0 enabled on the servers but 1.2 is. I ended up getting the commands for curl.exe to work in a Powershell script with variables, etc. – user2073183 Nov 14 '20 at 18:17
  • Glad to hear that you have it working. I would love to hear from anyone that reads this on the difference between cURL vs iwr from a networking perspective. – Michael Erpenbeck Nov 14 '20 at 22:17
  • Just another piece of information: I've been encountering the same problem with iwr over my home ISP (comcast) When the TLS settings did not work for me, just for giggles I tried doing the same thing over my T-Mobile hotspot and it worked without error. Doesn't solve the underlying problem but maybe speaks to the fact that it's not necessarily related to the endpoint's settings? – SteveT Nov 25 '20 at 20:13
  • I had a similar issue. After trying a workaround which simulates the SkipCertificateCheck argument, I ended up with downloading Powershell 7, and using it instead of version 5. – Zsolti Jan 17 '23 at 10:24

0 Answers0