API v2: How to update a host using the API?

I’m using the Foreman v2 API manage a bunch of hosts. I want to be able to change the host’s operating system based on the host group selected.

I’m making a PUT request to https::{foreman_domain}/api/hosts/{host_id} with the following JSON payload

{
   "id": "<host_id>",
   "host" : {
          "hostgroup_id": <hostgroup_id>
    }
}

But it only changes the host group of the host and doesn’t rebuild the host with the new operating system and architecture.

I tried gathering data from the host group using a GET request, and then using the operatingsystem_id and architecture_id, but I get a 422 response back.

Here’s the payload:

{
   "id": "<host_id>",
   "host" : {
          "operatingsystem_id": <os_id>,
          "architecture_id": <arch_id>,
          "hostgroup_id": <hostgroup_id>
    }
}

Any help would be appreciated!

HTTP 422 is used in the Foreman v2 API to indicate you sent some invalid data. It should include a JSON response explaining which fields were invalid.