Chapter 5. API requests in various languages
You can review the following examples of sending API requests to Red Hat Satellite from curl, Ruby, or Python.
5.1. Calling the API in curl Copy linkLink copied to clipboard!
You can use curl
with the Satellite API to perform various tasks.
Red Hat Satellite requires the use of HTTPS, and by default, a certificate for host identification. If you have not added the Satellite Server certificate as described in Section 4.1, “SSL authentication overview”, then you can use the --insecure
option to bypass certificate checks.
For user authentication, you can use the --user
option to provide Satellite user credentials in the form --user My_User_Name:_My_Password
. If you do not include the password, the command prompts you to enter it. To reduce security risks, do not include the password as part of the command, because it then becomes part of your shell history. For simplicity, the examples in this section include the password.
Be aware that if you use the --silent
option, curl
does not display a progress meter or any error messages.
Examples in this chapter use the Python json.tool
module to format the output.
5.1.1. Passing JSON data to the API request Copy linkLink copied to clipboard!
You can pass data to Satellite Server with the API request. The data must be in JSON format. When specifying JSON data with the --data
option, you must set the following HTTP headers with the --header
option:
--header "Accept:application/json" \ --header "Content-Type:application/json"
--header "Accept:application/json" \
--header "Content-Type:application/json"
Use one of the following options to include data with the --data
option.
JSON-formatted string
Enclose the quoted JSON-formatted data in curly braces {}
. When passing a value for a JSON type parameter, you must escape quotation marks "
with backslashes \
. For example, within curly braces, you must format "Example JSON Variable"
as \"Example JSON Variable\"
:
--data {"id":44, "smart_class_parameter":{"override":"true", "parameter_type":"json", "default_value":"{\"GRUB_CMDLINE_LINUX\": {\"audit\":\"1\",\"crashkernel\":\"true\"}}"}}
--data {"id":44, "smart_class_parameter":{"override":"true", "parameter_type":"json", "default_value":"{\"GRUB_CMDLINE_LINUX\": {\"audit\":\"1\",\"crashkernel\":\"true\"}}"}}
JSON-formatted file
The unquoted JSON-formatted data enclosed in a file and specified by the @
sign and the filename. For example:
--data @file.json
--data @file.json
Using external files for JSON formatted data has the following advantages:
- You can use your favorite text editor.
- You can use syntax checker to find and avoid mistakes.
- You can use tools to check the validity of JSON data or to reformat it.
Use the json_verify
tool to check the validity of the JSON file:
json_verify < file.json
$ json_verify < file.json
5.1.2. Retrieving a list of resources Copy linkLink copied to clipboard!
This section outlines how to use curl
with the Satellite 6 API to request information from Satellite. These examples include both requests and responses. Expect different results for each deployment.
5.1.2.1. Listing users Copy linkLink copied to clipboard!
This example is a basic request that returns a list of Satellite resources, Satellite users in this case. Such requests return a list of data wrapped in metadata, while other request types only return the actual object.
API request
curl \ --request GET \ --user My_User_Name:My_Password \ https://satellite.example.com/api/users \ | python3 -m json.tool
$ curl \
--request GET \
--user My_User_Name:My_Password \
https://satellite.example.com/api/users \
| python3 -m json.tool
API response
5.1.3. Creating and modifying resources Copy linkLink copied to clipboard!
You can use curl
to manipulate resources on your Satellite Server. API calls to Satellite require data in json
format. For more information, see Section 5.1.1, “Passing JSON data to the API request”.
5.1.4. Creating a user Copy linkLink copied to clipboard!
Use this procedure to create a user.
API request
5.1.5. Modifying a user Copy linkLink copied to clipboard!
This example modifies given name and login of the test_user
that was created in API request.
API request
5.2. Calling the API in Ruby Copy linkLink copied to clipboard!
You can use Ruby with the Satellite API to perform various tasks.
These are example scripts and commands. Ensure you review these scripts carefully before use, and replace any variables, user names, passwords, and other information to suit your own deployment.
5.2.1. Creating objects by using Ruby Copy linkLink copied to clipboard!
This script connects to the Red Hat Satellite 6 API, creates an organization, and then creates three lifecycle environments in the organization. If the organization already exists, the script uses that organization. If any of the lifecycle environments already exist in the organization, the script raises an error and quits.
5.2.2. Using apipie bindings with Ruby Copy linkLink copied to clipboard!
Apipie bindings are the Ruby bindings for apipie documented API calls. They fetch and cache the API definition from Satellite and then generate API calls as needed.
5.3. Calling the API in Python Copy linkLink copied to clipboard!
You can use Python with the Satellite API to perform various tasks.
These are example scripts and commands. Ensure you review these scripts carefully before use, and replace any variables, user names, passwords, and other information to suit your own deployment.
Example scripts in this section do not use SSL verification for interacting with the REST API.
5.3.1. Creating objects by using Python Copy linkLink copied to clipboard!
This script connects to the Red Hat Satellite 6 API, creates an organization, and then creates three environments in the organization. If the organization already exists, the script uses that organization. If any of the environments already exist in the organization, the script raises an error and quits.
5.3.2. Retrieving resource information by using Python Copy linkLink copied to clipboard!
This is an example script that uses Python for various API requests.