I am not able to add a puppetclass via the Foreman API v2

Hello Foreman Community,

I am trying to add a puppetclass to a host through the Foreman API v2, but I am not able to.
I am using foreman-1.15.2-1. I presume the API is also 1.15

I already checked this link and I’m trying to figure out why it doesn’t work for me.

The puppet class I’m trying to add has an id of 43. I checked it by doing a “GET /api/puppetclasses”.
The host has an id of 48. I checked it by doing a “GET /api/hosts”

The way I’m sending the POST request is:
curl -X POST -d “{\“43\”:2}” -H ‘Accept:version=2’ -H “Content-Type:application/json” -H “Accept:application/json” --header ‘Authorization: Basic <My_Authorization>’ --url https://<My_Foreman_Server>/api/v2/hosts/48/puppetclass_ids/

This returns the following error:
{
“error”: {“message”:“ERROR: insert or update on table “host_classes” violates foreign key constraint “host_classes_puppetclass_id_fk”\nDETAIL: Key (puppetclass_id)=(0) is not present in table “puppetclasses”.\n”}
}

Other PUT,POST,DELETE requests work fine. For example I am able to override parameter values, I am able to delete puppet classes from hosts etc. I just can’t get this to work.

My knowledge of HTTP and REST APIs is somewhat limited and I am not sure what I am doing wrong.

Any help would be appreciated :slight_smile:

Hello,
the data in your request are wrong. It should be -d '{"puppetclass_id": 2}'. The example in the Joseph’s original post really contains "puppetclass_id", no replacement needed.

I’m not sure if that works with API v2 though. The endpoint might have changed.

1 Like

Yes that solved the problem. Here is the full command:

curl -X POST -d ‘{“puppetclass_id”: 43}’ -H ‘Accept:version=2’ -H “Content-Type:application/json” -H “Accept:application/json” --header ‘Authorization: Basic <My_Authorization>’ --url https://<My_Foreman_Server>/api/v2/hosts/48/puppetclass_ids/

Thank you so much :smiley: