Skip to content
Snippets Groups Projects
Commit be92cc78 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

v0.5 a few more statuscode checks to complete features

parent 68309522
Branches
No related tags found
No related merge requests found
...@@ -6,7 +6,13 @@ The specialties of this component are ...@@ -6,7 +6,13 @@ The specialties of this component are
* It was build to simplify http calls and use it in scripts * It was build to simplify http calls and use it in scripts
* After making a request the response stays is in memory. There is no output to any file to grep something or to cleanup after usage. * After making a request the response stays is in memory. There is no output to any file to grep something or to cleanup after usage.
* Its functions feel a bit like class methods, i.e. http.getResponse to get tehe response body or http.getResponseHeader for the Http response header * Its functions feel a bit like class methods, i.e. http.getResponse to get tehe response body or http.getResponseHeader for the Http response header
* The response an be stored ... and reimportant later. After import you can use the http.get* functions to fetch results from thew former request. * The response can be stored ... and reimported later. After import you can use the http.get* functions to fetch results from the former request.
* This component wraps curl - ist supports any http method
* works with anonymous requests and Basic Authentication
Source: https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client
License: GNU GPL 3
## Requirements ## ## Requirements ##
...@@ -119,14 +125,25 @@ INSTRUCTION: ...@@ -119,14 +125,25 @@ INSTRUCTION:
http.getResponseHeader http.getResponseHeader
Get The http reponse header Get The http reponse header
- check http status code
http.getStatuscode http.getStatuscode
Get the http status code of a request Get the http status code of a request
http.isok http.isOk
Check if the http response code is a 2xx Check if the http response code is a 2xx
http.isredirect http.isRedirect
Check if the http response code is a 333xx Check if the http response code is a 3xx
http.isError
Check if the http response code is a 4xx or 5xx
http.isClientError
Check if the http response code is a 4xx
http.isServerError
Check if the http response code is a 5xx
http.getRequestAge http.getRequestAge
Get the age of the request in seconds. Get the age of the request in seconds.
...@@ -136,7 +153,6 @@ INSTRUCTION: ...@@ -136,7 +153,6 @@ INSTRUCTION:
http.getRequestTs http.getRequestTs
Get the Unix timestamp of the request Get the Unix timestamp of the request
- import/ export - import/ export
http.responseExport [FILE] http.responseExport [FILE]
......
...@@ -3,15 +3,17 @@ ...@@ -3,15 +3,17 @@
# #
# REST API CLIENT USING CURL # REST API CLIENT USING CURL
# #
# requires # REQUIREMENTS
# - Bash (Linux or MS Windows i.e with Cygwin)
# - curl # - curl
# - config file with endpoint, api user, password # - sha1sum (optional; for export functionality with AUTOFILE only)
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# (1) source this script # (1) source this script
# (2) enter "http.showhelp" to get a list ov available commands # (2) enter "http.help" to get a list of available commands
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# 2020-02-07 v0.2 axel.hahn@iml.unibe.ch BETABETA # 2020-02-07 v0.2 axel.hahn@iml.unibe.ch BETABETA
# 2020-02-12 v0.4 axel.hahn@iml.unibe.ch Caching # 2020-02-12 v0.4 axel.hahn@iml.unibe.ch Caching
# 2020-03-02 v0.5 axel.hahn@iml.unibe.ch a few more response check functions
# ====================================================================== # ======================================================================
# --- fetch incoming params # --- fetch incoming params
...@@ -20,12 +22,12 @@ ...@@ -20,12 +22,12 @@
ApiUrl=$3 ApiUrl=$3
Body="$4" Body="$4"
http_cfg__about="Bash REST API client v0.5"
http_cfg__about="Bash REST API client v0.4"
typeset -i http_cfg__debug=0 typeset -i http_cfg__debug=0
typeset -i http_cfg__cacheTtl=0 typeset -i http_cfg__cacheTtl=0
http_cfg__cacheDir=/var/tmp/http-cache http_cfg__cacheDir=/var/tmp/http-cache
http_cfg__UA="${http_cfg__about}" http_cfg__UA="${http_cfg__about}"
http_cfg__prjurl="https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client"
# --- curl meta infos to collect # --- curl meta infos to collect
# see variables in man curl --write-out param # see variables in man curl --write-out param
...@@ -224,6 +226,7 @@ EOH ...@@ -224,6 +226,7 @@ EOH
} }
# load a config file
function http.loadcfg(){ function http.loadcfg(){
http._wd "${FUNCNAME[0]}($1) !!! DEPRECATED !!!" http._wd "${FUNCNAME[0]}($1) !!! DEPRECATED !!!"
# reset expected vars from config # reset expected vars from config
...@@ -344,6 +347,14 @@ EOH ...@@ -344,6 +347,14 @@ EOH
echo "${http_resp__all}" echo "${http_resp__all}"
} }
# get Http status as string OK|Redirect|Error
function http.getStatus(){
http._wd "${FUNCNAME[0]}($1)"
http.isOk >/dev/null && echo OK
http.isRedirect >/dev/null && echo Redirect
http.isError >/dev/null && echo Error
}
# get Http status code of the request as 3 digit number # get Http status code of the request as 3 digit number
function http.getStatuscode(){ function http.getStatuscode(){
http._wd "${FUNCNAME[0]}($1)" http._wd "${FUNCNAME[0]}($1)"
...@@ -356,16 +367,32 @@ EOH ...@@ -356,16 +367,32 @@ EOH
# Additionally you can verify the return code # Additionally you can verify the return code
# $? -eq 0 means YES # $? -eq 0 means YES
# $? -ne 0 means NO # $? -ne 0 means NO
function http.isok(){ function http.isOk(){
http._wd "${FUNCNAME[0]}($1)" http._wd "${FUNCNAME[0]}($1)"
http.getStatuscode | grep '2[0-9][0-9]' http.getStatuscode | grep '2[0-9][0-9]'
} }
# was the repsonse a redirect? # was the repsonse a redirect?
function http.isredirect(){ function http.isRedirect(){
http._wd "${FUNCNAME[0]}($1)" http._wd "${FUNCNAME[0]}($1)"
http.getStatuscode | grep '3[0-9][0-9]' http.getStatuscode | grep '3[0-9][0-9]'
} }
# was the repsonse a client error (4xx or 5xx)
function http.isError(){
http._wd "${FUNCNAME[0]}($1)"
http.getStatuscode | grep '[45][0-9][0-9]'
}
# was the repsonse a client error (4xx)
function http.isClientError(){
http._wd "${FUNCNAME[0]}($1)"
http.getStatuscode | grep '4[0-9][0-9]'
}
# was the repsonse a client error (5xx)
function http.isServerError(){
http._wd "${FUNCNAME[0]}($1)"
http.getStatuscode | grep '5[0-9][0-9]'
}
# dump information about request and response # dump information about request and response
function http.dump(){ function http.dump(){
...@@ -531,6 +558,14 @@ EOH ...@@ -531,6 +558,14 @@ EOH
function http.help(){ function http.help(){
cat <<EOH cat <<EOH
$http_cfg__about
This is a bash solution to script REST API calls.
Source:$http_cfg__prjurl
License: GNU GPL 3
INSTRUCTION: INSTRUCTION:
- Source the file once - Source the file once
...@@ -544,7 +579,6 @@ INSTRUCTION: ...@@ -544,7 +579,6 @@ INSTRUCTION:
Enable or disable debugging infos during processing. It is written Enable or disable debugging infos during processing. It is written
to STDERR. to STDERR.
- initialize a request - initialize a request
setAuth AUTH:PASSWORD setAuth AUTH:PASSWORD
...@@ -610,14 +644,25 @@ INSTRUCTION: ...@@ -610,14 +644,25 @@ INSTRUCTION:
http.getResponseHeader http.getResponseHeader
Get The http reponse header Get The http reponse header
- check http status code
http.getStatuscode http.getStatuscode
Get the http status code of a request Get the http status code of a request
http.isok http.isOk
Check if the http response code is a 2xx Check if the http response code is a 2xx
http.isredirect http.isRedirect
Check if the http response code is a 333xx Check if the http response code is a 3xx
http.isError
Check if the http response code is a 4xx or 5xx
http.isClientError
Check if the http response code is a 4xx
http.isServerError
Check if the http response code is a 5xx
http.getRequestAge http.getRequestAge
Get the age of the request in seconds. Get the age of the request in seconds.
...@@ -663,14 +708,4 @@ EOH ...@@ -663,14 +708,4 @@ EOH
http.init http.init
if [ $# -ge 3 ]; then
http.loadCfg "${1}"
http.setMethod "${2}"
http.setUrl "${3}"
http.setBody "${4}"
http.makeRequest
fi
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment