Search code examples
powershellcurlartifactory

cUrl vs Invoke-WebRequest


Can anybody explain to me why cUrl (the real cUrl) works but Invoke-WebRequest doesn’t? Same machine, same variables. To me it looks like they should both be doing the same thing, uploading a file to jfrog Artifactory.

$headers = @{
  'X-JFrog-Art-Api' = $apiKey
  "Content-Type" = "application/json"
  "Accept" = "application/json"
}

Invoke-WebRequest -InFile $file -Method Put -Uri "$ARTIFACTORY_HOST/third-party/test/readme.md" -Headers $headers -Verbose

This PowerShell doesn't work.

curl -T readme.md "${ARTIFACTORY_HOST}/third-party/test/readme.md " \
-H "X-JFrog-Art-Api: ${apiKey}" \
-H "Content-Type: application/json" \
-H "Accept: application/json"

cUrl works.

PowerShell fails with

Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At line:1 char:1
+ Invoke-WebRequest -InFile $file -Method Put -Uri "https:// ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Solution

  • Turns out PowerShell defaults to the wrong TLS version and needs to be specifically told to use 1.2

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

    Put that in front of the Invoke-WebRequest and all is fine.