Hestia Api :Python connector

Hi I was trying to create a python connector for creating users in hestia, while connecting to the API I’m getting some error like this

import requests
from urllib.parse import urlencode

Admin Credentials

hst_hostname = ‘ip’
hst_port = 8083
hst_username = ‘admin’
hst_password = ‘password’
hst_returncode = ‘yes’
hst_command = ‘v-add-user’

New account details

username = ‘demo’
password = ‘demo’
email = ‘[email protected]
package = ‘default’
first_name = ‘Demo’
last_name = ‘M’

Prepare data for API request

data = {
‘hash’: ‘access key’,
‘user’: hst_username,
‘password’: hst_password,
‘returncode’: hst_returncode,
‘cmd’: hst_command,
‘arg1’: username,
‘arg2’: password,
‘arg3’: email,
‘arg4’: package,
‘arg5’: first_name,
‘arg6’: last_name
}

URL encode the data

encoded_data = urlencode(data)

Send POST request to HestiaCP API

url = f’https://{hst_hostname}:{hst_port}/api/’
print(“url>>>>>>”, url)
try:
response = requests.post(url, data=encoded_data, verify=False)
print(f"Request data: {encoded_data}“)
print(f"Response status code: {response.status_code}”)
print(f"Response text: {response.text}“)
print(response.text)
print(“0 means successful”)
except requests.exceptions.RequestException as e:
print(f"Error: {e}”)

error:
Response status code: 405
Response text: Error: data received is null or invalid, check REST API | Hestia Control Panel
Error: data received is null or invalid, check REST API | Hestia Control Panel
0 means successful

Process finished with exit code 0

There are a few issues with the code you’ve provided that I noticed.

I believe you need to choose if you’re going to authenticate with access key or username password see here and here. Not using both, the url will probably be malformed.

I don’t think you need to use urlencode() if you’re passing a library with the requests.post method.

‘user’: hst_username,
‘password’: hst_password,

Can be removed use the access hash:secrethash instead and it should work fine

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.