Ansible Tower API calls work from Postman but not in Powershell - powershell

I'm using a personal access token to make a call to /api/v2/hosts. In the header, I have "Authorization: bearer <personal access token". In postman it works just fine, I get a list of the hosts. But in Powershell, using Invoke-RestMethod fails with
Invoke-RestMethod : {"detail":"Authentication credentials were not provided. To establish a login session, visit /api/login/."}
Does anyone have any idea why this would be the case?
Here's the code: all values are non-sensitive. They are throwaway:
Here's the relevant code, non of the information is sensitive, it's non-production throwaway:
$headers = #{ Accept = "application/json"; Authorization = "bearer YqQoPFTD7GRZqB3XxAvVlZ2SUWxZGc"; "Accept-Encoding" = "gzip, deflate, br"}
$hosts=Invoke-RestMethod "https://10.100.235.4/api/v2/hosts" -Method Get -Headers $headers

The problem is that the response was a 301, and after the redirect the headers were being dropped. Apparently Powershell 7 has an option to stop this from happening, so I think that's the answer.

Related

Invoke Ansible API Token via Powershell

I have created a Token for Admin account. Now I am trying to invoke some API in ansible using the below
$headers=#{
Authorization="Bearer <token>"
}
$response=Invoke-WebRequest -Method GET -Uri "https://ansible.xyz.com/api/v2/job_templates" -Headers $headers -ContentType "application/json"
When I do run the above I get the following error:
Authentication credentials were not provided. To establish a login session, visit /api/login/.
I don't understand when I use Token to authenticate why it's still showing an error.
When I use the same auth token in postman or curl it's working fine.

Cant' Access API with Powershell but can access using Postman, Python, or cURL

I'm trying to get data back from an API online using Powershell with the command Invoke-WebRequest -UseBasicParsing $request_string -Method Get -Headers $headers) but am getting back Invoke-WebRequest : The remote server returned an error: (403) Forbidden.
I am supplying a $headers dictionary. Strangely, I can access the API using Postman, Python, and cURL. It's only when using Powershell commands that I get the 403 error. In fact, I used Postman's Code Snippet feature to generate my Powershell code, and it still doensn't work! Postman's Powershell Code Snippet was:
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer {removed for security}")
$response = Invoke-RestMethod '{removed for security}' -Method 'GET' -Headers $headers
$response | ConvertTo-Json
To recap, both Invoke-WebRequest and Invoke-RestMethod don't work.
Any help here is much appreciated.
Thanks!
Figured it out on my own.
The API vendor enforced https requirement instead of just http. Apparently, Postman, Python, and cURL can figure that out on their own and change the request accordingly, but Powershell cannot.

Invoke-RestMethod returning 401 - Unauthorized in Powershell 7

I'm running into an issue trying to use the Invoke-RestMethod command in PowerShell 7. I can get it to run fine in PowerShell 5.1, but 7 gives me a 401 - Unauthorized message.
Here's the command for PowerShell 5.1:
Invoke-RestMethod "http://internalServer/api/job?name=testJob" -Method GET -UseDefaultCredentials -ContentType "application/JSON"
Here's the command for PowerShell 7:
Invoke-RestMethod "http://internalServer/api/job?name=testJob" -Method GET -UseDefaultCredentials -ContentType "application/JSON" -AllowUnencryptedAuthentication
The api is hosted on an internal server that uses Windows Authentication. When I track the requests through Fiddler, both commands seem to get the 401 response, but PowerShell 5.1 uses the response to generate an Authorization: Negotiate YII{token} header whereas PowerShell 7 stops and returns an error. Has anyone else encountered this before?
As indicated in the comments, there is a redirect going on here. By default, authentication won't survive a redirect, but you can control that with the -PreserveAuthorizationOnRedirect parameter to Invoke-RestMethod:
$irmParams = #{
Uri = "http://internalServer/api/job?name=testJob"
Method = 'GET'
UseDefaultCredentials = $true
ContentType = 'application/json'
PreserveAuthorizationOnRedirect = $true # <== Should be your solution
AllowUnencryptedAuthentication = $true # <=== You should not be using this :)
}
Invoke-RestMethod #irmParams
Thanks to some additional legwork by OP, -PreserveAuthorizationOnRedirect:
Will only keep the authentication headers for requests made to a Uri that includes the original Uri up to the last /. What the documentation doesn't include is that the subsequent Uri's must also match the case of the original Uri.
In OP's case, the redirection was changing the case of the original Uri, thus breaking the authentication on redirect even when they specified -PreserveAuthorizationOnRedirect.

Powershell and Rest API Postmark

when I send a request through powershell to rest api Postmarkapp I have these errors
When use metod get
Invoke-RestMethod : Cannot send a content-body with this verb-type.
When use metod post
Server Error in '/' Application. The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /deliverystats
Script
$Uri = 'https://api.postmarkapp.com/deliverystats'
Invoke-RestMethod $Uri -Method Post -Headers #{'X-Postmark-Server-Token' =" Token" } -ContentType "application/json" |
The script you provided wasn't complete - it ends with a |.
A valid token is required before executing a request or you'll get this error:
Invoke-RestMethod : {"ErrorCode":10,"Message":"No Account or Server API tokens were supplied in the HTTP headers. Please add a header for either
X-Postmark-Server-Token or X-Postmark-Account-Token."}
Your code had ' Token', which is a constant and is probably not a valid value for the X-Postmark-Server-Token or X-Postmark-Account-Token header. You didn't show how $Token was set, but it probably should have been something like this:
$Token = 'xxxxxxxxxxxxx' #your account specific token
$uri = 'https://api.postmarkapp.com/deliverystats'
Then add the headers like this (with a $ before Token):
Invoke-RestMethod $Uri -Method Get -Headers #{'X-Postmark-Server-Token' ="$Token" } -ContentType "application/json"

Unable to get response when Head method is used with Invoke-RestMethod

I am running the following within PowerShell with the Head method but I am not getting any response. The expected output is in encoded format.
Invoke-RestMethod -Uri 'http://localhost:2375/containers/45e97476b2f5/archive?path=/home/test/' -Method Head
When I run the same REST API request with CURL or SOAP UI, I get expected output as below.
HTTP/1.1 200 OK
Content-Type: application/x-tar
Server: Docker/1.12.3 (linux)
X-Docker-Container-Path-Stat: eyJuYW1lIjoidGVzdCIsInNpemUiOjYsIm1vZGUiOjIxNDc0ODQxNDEsIm10aW1lIjoiMjAxNy0wMy0wN1QyMzoyMToyNC40MD
Any help?
Invoke-RestMethod is for interacting with a Restful web service and as such it does not return the headers of your request even when you specify the Head method.
You should instead use Invoke-WebRequest which does bring back the headers, and other properties (with headers then being in the .headers property).
For example:
$Web = Invoke-WebRequest -URI 'http://localhost:2375/containers/45e97476b2f5/archive?path=/home/test/' -Method Head
$Web.Headers
I am able to get response using Invoke-WebRequest.