Need some help with the API

Problem:
I want to next a location under an existing one via the API.
I tried the following JSON:

{
  "location[name]": "Test+API",
  "location[parent_id]": "7"
}

I also tried the following:

{
  "location[name]": "Test API",
  "location[parent_id]": "7"
}

{
  "location_id": "12",
  "location[parent_id]": "7"
}

{
  "location_id": 12,
  "location[parent_id]": "7"
}

{
  "location_id": 12,
  "location[parent_id]": 7
}

{
  "location_id": 12,
  "location[parent_id]": 7
}

with location_id instead of the location[name], with the id of the parent without quotes etc.
It doesn’t work!

I checked in my Satellite Server from Firefox what is the API request and it looks like it’s like the one I posted above. From Satellite (of course) it works, when I run this with curl it doesn’t.
The curl is called like this:

curl --header "Content-Type:application/json" --request PUT --user admin:password --data @edit_location.json https://foreman.example.com/api/locations/12

Expected outcome:
API call should next my location under the location with ID 7.

What do I miss?

Looking at the API docs, you are passing the data wrong. “location” is a required filed and must be a hash, “location[parent_id]” in the docs means that parent_id is a field in the hash. Your data should look something like this:

{
  "location_id": 12,
  "location": {
    "parent_id": 7
  }
}

No guarantee this is valid json, since I just freestyled it :wink: But I think you should get the idea.
In the API docs, you can see the data type for each field and if it is required or not.

2 Likes

Many thanks for the explanation!

Foreman API documentation is a mystery for me. What you mention about the hash etc was a not clear until now!