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.'
    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)
  • Moderator

    @maddyred You might want to take a look at this topic: https://forums.fogproject.org/topic/15848/api-creating-host

  • @sebastian-roth Hi, I tried with jsondumps(data) and it is now throwing HTTP 500 error. Need some help on registering host with api.

  • Moderator

    @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?