API - Create Host "error": "Required database field is empty"
-
Hi, I am trying to create a new host from api in pythonwith post to data to create a host it gives me this error: “error”: “Required database field is empty”
My python code Api looks like this:
import requests import json session = requests.Session() url = 'http://x.x.x.x.' userToken = 'ZDJkMDU4ODEyNDA2YTVlZDZhYjkxOGY3OGM2NDZiNjhiZmRkYjJlMTQ4Mzc1Y2FkMjQ2M2ViNGRkMDNjN2QxMjkwOThhM2Q4NTQ1ZmI3M2JhNmY5MGNiY2ZjYjAxMTVhNzdiZmMwZWE0ZjgzZjI0NzM5N2NkOTEzZjJlNzBjOTk=' apiToken = 'ZTRkM2MxNWZlMDFmNWY3ZTJlNDBmM2MxN2Q0ZjFhOTI0ODc0NzExOWJlYmI0NmJiYTk5MzM1NDUxMmU1Yjg1ZThhOGM4MWM4ZDIxNDg1NGFlNmRiYmFiZWRmOGFkMWFjYTMwOGExMjA2ZGVkN2FiNDM1ZmU2YzcyY2Q3NjVlZDg=' session.headers.update({'fog-user-token': userToken, 'fog-api-token':apiToken, 'Content-Type': 'application/json' }) data = { "name": "12test host", "description": "test hosts", "macs": "c4:90:19:9c:6b:ea", "imageID": "22", "imagename": "Windows_Server_2019" } response = session.post(url + '/fog/host/create',data=data) print(response.text)
-
@maddyred The major issue in your script is that
data
is a simple python object and not JSON yet. Change the post line into this:response = session.post(url + '/fog/host/create',data=json.dumps(data))
BUT host creation still doesn’t work with your script for several reasons:
- You shouldn’t use spaces in hostnames!
- The
"macs"
parameter prevents from creating the new host object because it’s not the correct way to specify the MAC address. I have not used the API in a long time and so I am not sure what’s the correct way to do it.
@JJ-Fullmer Although I have played with it I have not found a way to add a host object including the MAC address association. Can you give us some hints on how to do this. What does the JSON string have to look like for creating a host including MAC address?
-
@sebastian-roth Hi, I tried with jsondumps(data) and it is now throwing HTTP 500 error. Need some help on registering host with api.
-
@maddyred You might want to take a look at this topic: https://forums.fogproject.org/topic/15848/api-creating-host