Python Requests library returns empty array (vCenter)

46 views Asked by At

I am currently developing some Python code to automate stuff in our companies vCenter.

For development I use Insomnia as a API Client and everything works as expected.

Now to my code:

# Authenticate to vCenter API
import requests as r 
import time

url = "https://vcenter.domain.net/rest/com/vmware/cis/session"
authHeader = {
    "Accept": "application/json",
    "Authorization": "Basic username:password"
}
authResponse = r.request("POST", url, headers=authHeader)

authToken = authResponse.json()['value']

In the above code I authenticate to vCenter and save the authentication Token as authToken. In the next part I am rebuilding the request and use the authToken variable in the header.

# Get VM List
url = "https://vcenter.domain.net/api/vcenter/vm"

headers = {
    "vmware-api-session-id": authToken
}
payload = ""

tokenResponse = r.get(url, data=payload, headers=headers)
print(tokenResponse.text)
print(tokenResponse.status_code)

The response is just:

[user@domain@host temp]$ python3 vcenter-guestcustomization.py []
200

When I investigate the tokenResponse.status_code I will get a 200 (as seen in the response).

When I use the code from the generator everything works as expected. I noticed, that the sessiontoken was hardcoded in the sample code.

I am only changing the headers dict;

  • I am replacing the "vmware-api-session-id" value from the variable "authToken" to the harcoded string.
  • it works

I got had a similar issue in a different API a few days ago.

What am I doing wrong?

0

There are 0 answers