'curl -k or --insecure equivalent to Powershell Invoke-WebRequest not working

I am trying to execute:

$postParams = @{user='myUser';password='myPass'};
Invoke-WebRequest -Uri https://MYURL -Method POST -Body $postParams;

And I am always getting 403 forbidden, in ubuntu or git terminal this works like a charm (with -k or --insecure):

curl -k -X POST "https://MYURL" -H "accept: */*" -H "Content-Type: application/json" -d "{ \"user\" : \"myUser\", \"password\" : \"myPass\"}"

or

curl -X POST "https://MYURL" -H "accept: */*" -H "Content-Type: application/json" -d "{ \"user\" : \"myUser\", \"password\" : \"myPass\"}" --insecure

After some research I tried two solutions:

Solution #1:

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true };

$postParams = @{user='myUser';password='myPass'};
Invoke-WebRequest -Uri https://MYURL -Method POST -Body $postParams;

Did not work still getting 403 forbidden.

Solution #2:

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true };

$postParams = @{user='myUser';password='myPass'};
Invoke-WebRequest -Uri https://MYURL -Method POST -Body $postParams;

add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy;

$postParams = @{user='myUser';password='myPass'};
Invoke-WebRequest -Uri https://MYURL -Method POST -Body $postParams;

Did not work either, still getting 403 forbidden.

Any clues of what I might be doing wrong? Since it works ok in ubuntu and git terminal with -k or --insecure.



Solution 1:[1]

The short answer, is Invoke-WebRequest is not an attempt to replace curl and simply does not support this feature.

So far, the only way I found without using other PS modules is something that should never be done (credit goes to: ref)

Run this before running the Invoke-WebRequest command. It may popup an internet explorer lock warning, but still lets the PS command run, ignoring an expired cert.

add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPol

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Efren