Preparation

A required step is sourcing the script to make the http.* function available in the current shell.

# --- source script
> . ./http.class.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.

# --- 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.

# --- 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].

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."

POST request with Json data

This snippet sends the result of a monitor check to the Json API of a Icinga sattelite. It uses jo to generate valid JSON.

data="$( jo  \
    check_source=${CHECK}                          \
    check_command="""${myFullscript} $myparams"""  \
    exit_status=$rc                                \
    ttl=$checkInterval                             \
    execution_start=$iTsStart                      \
    execution_end=$iTsEnd                          \
    performance_data="${outPerfdata}"              \
    plugin_output="""${output}""" 
)"

http.makeRequest POST actions/process-check-result?service=${CHECK}!${SERVICE} "$data"

Authentication with JWT token

The snippet shows how to fetch a token first that will be added to be used in all following requests.

# ---------- settings
URL=https://www.example.com/
USER=...
PASSWORD=...

# ---------- init
http.init
http.setBaseUrl "${URL}"

# ---------- 1st request: get a token
http.setAuth "${USER}:${PASSWORD}"
http.makeRequest GET "token"

# fetch token 
ACCESS_TOKEN=$(http.getResponse | jq -r .access_token)

# Set token for authorization
http.setAuthorization "Bearer" "${ACCESS_TOKEN}"

# Unset basic auth
http.setAuth

# ---------- Requests to app or api
http.makeRequest GET "api"
http.makeRequest GET "app"

Gitlab access token

Gitlab uses the value PRIVATE-TOKEN: <token> in the request header for authorization. Here we need http.addHeader to add a custom header line.

URL='https://gitlab.example.com/api/v4'
TOKEN='<add-your-token-here>' # glpat-**********

# ---------- init

http.init
http.addHeader "PRIVATE-TOKEN: $TOKEN"
http.setBaseUrl "$URL"

# ---------- Requests to api

http.makeRequest /users
http.getResponse | jq

http.makeRequest /projects
http.getResponse | jq