# Preparation A required step is sourcing the script to make the http.* function available in the current shell. ```sh # --- source script > . ./rest-api-client.sh ``` Then you can follow the examples. # A first example This is quick introduction: we make a request and have a look to a few results. ```sh # --- init ... to flush anything of a request that was made before > http.init # --- make a GET request ... > http.makeRequest GET http://www.iml.unibe.ch # OR "http.makeRequest [url]" ... GET is the default # --- get the result > http.getStatuscode 301 > http.getStatus Redirect # --- view the http response header > http.getResponseHeader HTTP/1.1 301 Moved Permanently Date: Tue, 11 Jan 2022 12:26:37 GMT Server: Apache Location: https://www.iml.unibe.ch/ Content-Length: 233 Connection: close Content-Type: text/html; charset=iso-8859-1 # --- view curl data > http.getResponseData http_code:301 http_connect:000 local_ip:172.30.158.65 local_port:58512 num_connects:1 num_redirects:0 redirect_url:https://www.iml.unibe.ch/ remote_ip:130.92.30.80 remote_port:80 size_download:233 size_header:209 size_request:139 size_upload:0 speed_download:3405 speed_upload:0 ssl_verify_result:0 time_appconnect:0.000000 time_connect:0.049925 time_namelookup:0.024735 time_pretransfer:0.050054 time_redirect:0.000000 time_starttransfer:0.068274 time_total:0.068411 url_effective:http://www.iml.unibe.ch/ ``` # Icinga2 API In that example we have a config for an api access with url, user and password. The docs url is not required for functionality. It will be shown on errors. Imagine, to deploy such a config with Ansible/ Puppet/ on all hosts. ```sh # --- data in a config file that is sourced by your script: RestApiBaseUrl="https://icinga-endpoint.example.com:5665/v1/" RestApiUser="icingaapi" RestApiPassword="password-of-Icinga-Api-User" RestApiDocs="https://icinga.com/docs/icinga2/latest/doc/12-icinga2-api/" ``` And now let's access the Icinga API and try to find out if the current host is available in Icinga2. Therefor we make a GET request to `[API url]/objects/hosts/[my hostname]`. ```sh myHost=( hostname -f ) # --- init: http.init http.setAuth "${RestApiUser}:${RestApiPassword}" http.setBaseUrl "${RestApiBaseUrl}" http.setDocs "${RestApiDocs}" # --- check if my host exists in the monitoring # Because we set a base url we can operate with the part behind it http.makeRequest GET "objects/hosts/${myHost}" # --- show http response body (some JSON) http.getResponse # set return code of GET action ... $? is 0 on success (2xx status code) http.isOk >/dev/null if [ $? -ne 0 ]; then if [ "$( http.getStatuscode )" = "000" ]; then echo "ERROR: Unable to reach the Icinga node." exit 1 fi echo "ERROR: host object for ${myHost} is not available on Icinga service (yet) - Status: $( http.getStatuscode )" exit 1 fi echo "OK, ${myHost} was found." ```