Issue
When using the following curl command I get a 200 OK response that provides the auth token I need for future requests. However, when I try to replicate the same thing in PowerShell using the invoke-restmethod below, I get no response at all.
I've tried turning on verbose and it just says 0 bytes returned. I've also tried using -Timeoutsec and that makes no difference. I've also verified the base64 string for the user and pass is the same and is correct. I also know I'm hitting the API, because I can touch other endpoints and get 401 unauthorized.
I've also tried using POSTMan and using the feature that lets you past the cURL command in and it'll build the request for you and I get the same empty/null response.
I've been able to get it working in Python without issue as well.
Would really appreciate some help with this, as I'm not sure what I'm missing here.
Curl call (200 OK):
curl -qgsSkH --no-progress-bar --header "Authorization: Basic SDFLKJSLKDJFLKJWERFV3Fh1RHZjIyQl" -D auth.txt -F form=foo https://10.10.10.10:443/ENDPOINT/v6/auth/login -vvv**
Python Code which also works:
import requests
import base64
base_url = 'https://server.domain.com/ENDPOINT/v6/auth/login'
username = 'APIAccount'
password = 'PASSWORD' # Since only alphanumeric characters are alloowed, this should be an extra long password.
credentials = (username + ':' + password).encode('utf-8')
base64_encoded_credentials = base64.b64encode(credentials).decode('utf-8')
headers = {
'Authorization': 'Basic ' + base64_encoded_credentials
}
resp = requests.post(base_url, headers=headers, verify=False)
print(headers)
print('Status:', resp.status_code)
PowerShell code (no output):
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$USER = "APIAccount"
$password = 'PASSWORD'
$HeaderAuth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $USER, $password)))
$SessionHeader = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$SessionHeader.Add('Authorization',('Basic {0}' -f $HeaderAuth))
$SessionHeader.Add('Accept','application/json')
$SessionHeader.Add('Content-Type','application/json')
$URL = "https://server.domain.com/ENDPOINT/v6/auth/login"
$APIResponse = Invoke-RestMethod -Method POST -Uri $URL -Headers $Sessionheader -Verbose -TimeoutSec 33
Solution
Using Invoke-WebRequest instead, did the trick.
Answered By - MrMr Answer Checked By - Terry (WPSolving Volunteer)