A set of class like functions with a <code>http.</code> prefix.
This is a bash solution to script REST API calls.
The specialties of this component are
@@ -7,8 +8,8 @@ The specialties of this component are
* It was build to simplify http calls and handle http response for 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.
* Its functions feel a bit like class methods, i.e. http.getResponse to get the response body or http.getResponseHeader for the Http response header
* This component wraps curl - ist supports any http method
* works with anonymous requests and Basic Authentication
* This component wraps curl - it supports any http method
* works with anonymous requests and authentication / authorization: (Basic authentication, Authorization header (tokens, NTLM) or any other request header variable
* The response can be stored ... and reimported later. After import you can use the http.get* functions to fetch results from the former request.
* Caching support for GET requests with a given TTL: if you repeat a request to the same url and ttl was not reached yet then you continue with the cached result
@@ -9,8 +9,8 @@ The specialties of this component are
* It was build to simplify http calls and handle http response for 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.
* Its functions feel a bit like class methods, i.e. http.getResponse to get the response body or http.getResponseHeader for the Http response header
* This component wraps curl - ist supports any http method
* works with anonymous requests and Basic Authentication
* This component wraps curl - it supports any http method
* works with anonymous requests and authentication / authorization: (Basic authentication, Authorization header (tokens, NTLM) or any other request header variable
* The response can be stored ... and reimported later. After import you can use the http.get* functions to fetch results from the former request.
* Caching support for GET requests with a given TTL: if you repeat a request to the same url and ttl was not reached yet then you continue with the cached result
You must source the rest-api-client.sh. Then its functions are usable in the current process.
The "methods" start with "http" dot "[method]".
```sh
#!/bin/bash
cd"$(dirname"$0")"||exit
. ./rest-api-client.sh ||exit 1
```
### Initialize a new request
```text
Start a new request. It resets internal vars of the last request.
```sh
http.init
```
### Make a request
### Define a request
```text
http.makeRequest 'http://www.example.com/'
```
If you make multiple requests to the same backend you can pre define settings for all your requests.
* Use authentication
* Instead of requesting a full url you can set a base url to handle relative urls only when making a request
* set Accept header or any other line in the request header
#### Authentication
If you access protected applications or APIs that require an authentication or authorization you can send credentials or tokens to login / authorize.
See also the examples in the next page that show different authorization types.
##### Basic authentication
You should use the function `http.setAuth "<USER>:<PASSWORD>"`.
Internal info: This will add `-u "<USER>:<PASSWORD>"` in your curl requests.
Parameters:
*`"<USER>:<PASSWORD>"`: give a single string with Username + `:` + password in clear text.
Remove basic auth:
Use `http.setAuth` (without parameters) to remove the -u parameter for the next request(s).
##### Authorization header
A few authorization mechanisms use the variable `Authorization` in the request header.
For those use `http.setAuthorization <TYPE> <TOKEN|HASH>`. It will insert the curl parameter -H `Authorization: <TYPE> <TOKEN|HASH>`.
Parameters:
* As TYPE you can use Basic|Bearer|Negotiate|...
* The 2nd param is the token or hashed "<USER>:<PASSWORD>". A token is added as is. If a password must be encoded/ crypted you need put the encoded string here.
Remove authorization header:
Use `http.setAuthorization` (without parameters) to remove the authorization header for the next request(s).
##### Other Authorizations
If a backend needs another Variable in the request header for authorization then there is the function `http.addHeader <HEADER_LINE>`. With it you can put multiple request header variables for all your next resquests - not only for authorizations.
Parameters:
*`"<HEADER_LINE>"` a single string with the complete line to add.
Example:
`http.addHeader "PRIVATE-TOKEN: glpat-12345678"`
Remove custom header:
There is no way to remove a single header line that was added once.
But you can use `http.init` to start over.
#### Set url
Define a base url that will be used as prefix when making a request to a relative url.
*`http.setBaseUrl "<base-url>"`
#### Other funtions
*`http.setAccept <ACCEPT>`
*`http.setDocs URL` http.setMethod METHOD Set a http method. Use an uppercase string for GET|POST|PUT|DELETE|...
*`http.addHeader HEADER_LINE` Add a header line to the request. This command can be repeated multiple times.
*`http.setDocs` Set a docs url. If set it will be shown as additional hint when a request fails.
## Make a request
To start the request use `http.makeRequest [[METHOD] [URL] [BODY]]`.
The parameters are optional. Without parameter the request will be started with given data in http.set* functions described above.
If minimum one param is given then they are handled:
* METHOD optional: set a method (must be uppercase) - see http.setMethod
* URL set a relative url - see http.setUrl
* BODY optional: set a body - see http.setBody
This stores the response in a variable and has no output.
### Use http.* functions
The request will be skipped and uses a cached content if ...
* METHOD is GET
* http.setCacheTtl set a value > 0
* the cache file exists and is younger than the given TTL
Now you can get its data, eg
### Request multiple urls
* http.getStatuscode - This returns the Http status code
There is a set of function to get infirmation about the sttaus of the request.
*`http.getStatuscode` returns the Http status code with 3 digit number
*`http.getStatus` returns a string - one of Ok|Redirect|Error
Additionally there are these status functions.
*`http.isOk` Check if the http response code is a 2xx
*`http.isRedirect` 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
They call http.getStatus and grep for a http status code. They show the status code and have returncode 0 if true. If you don't nedd the status code then redirect it to /dev/null.
Snippet:
```sh
http.makeRequest "/products"
if http.isOk > /dev/null;then
echo"Yep, it was OK"
fi
```
## Caching
You can store the response locally. It can be useful for requests with longer response time. Or maybe you jump between diffferent urls and want to cache them during the current script run.
Default: Caching is off
This feature requires an installed `sha1sum` binary.
*`http.setCacheTtl <SECONDS>` Enable caching for N seconds. Remark: only GET requests will be cached. Default: 0 (no caching)
*`http.setCacheFile "<FILENAME>"` Set a file where to read/ store a request; Default: empty; autogenerated file below /var/tmp/http-cache
*`http.flushCache` Delete all files in `/var/tmp/http-cache`
Snippet:
```sh
# set a caching time of 3 sec
http.setCacheTtl 3
# 1st request: uncached
time http.makeRequest "/users?per_page=100"
# real 0m0.533s
# user 0m0.093s
# sys 0m0.107s
sleep 2
# 2nd request: uses caching
time http.makeRequest "/users?per_page=100"
# real 0m0.020s <<--- caching is active
# user 0m0.022s
# sys 0m0.009s
sleep 2
# 3rd request: TTL 3 sec is over after 2 x sleeping 2 sec
time http.makeRequest "/users?per_page=100"
# Real 0m0.578s <<--- cache is expired - a new request was made
# user 0m0.097s
# sys 0m0.099s
# remove cached datA
http.flushCache
```
## Import/ Export
If you want to keep all response data of a request then you can store it to a file and import it later.
*`http.responseExport ["<FILE>"]`
*`http.responseImport "<FILE>"`
*`http.responseDelete "<FILE>"`
After importing an older Response you can apply all function to get response data or http status.
Additionally these functions are useful:
*`http.getRequestAge` Get age of the response in sec. It is especially useful after responseImport
*`http.getRequestTs` Get timestamp of the response as a Unix timestamp.
## Help: list all functions
You can run `http.help` to get an overwiew over all functions.
```text
#
@@ -41,7 +270,7 @@ $ . ./rest-api-client.sh
#
$ http.help
Bash REST API client v0.9
Bash REST API client v0.10
This is a bash solution to script REST API calls.
@@ -56,7 +285,7 @@ INSTRUCTION:
- Then you can run functions starting with "http."
http.init
Start a new request. It resets internal vars of the last response
Start a new request. It resets internal vars of the last request
(if there was one).
http.setDebug 0|1
@@ -65,52 +294,54 @@ INSTRUCTION:
- initialize a request
setAccept ACCEPT
setAccept "<ACCEPTHEADER>"
Set authentication with user and password for basic auth
Default: application/json
setAuth AUTH:PASSWORD
setAuth "<USER>:<PASSWORD>"
Set authentication with user and password for basic auth
Without given parameter, authentication is removed
setAuthorization TYPETOKEN|HASH
setAuthorization "<TYPE>" "<TOKEN|HASH>"
Set authentication with Authorization header.
As TYPE you can use Basic|Bearer|Negotiate|...
2nd param is the token or hased user+password
2nd param is the token or hashed user+password
Without given parameter, authorization is removed
http.setBody DATA
http.setBody "<DATA>"
set a body for POST/ PUT requests.
http.setBaseUrl URL
http.setBaseUrl "<URL>"
Set a base url to an api.
renmark:
Use http.setUrl to built a complete url.
http.setDocs URL
http.setDocs "<URL>"
Set a docs url. If set it will be shown as additional hint when a
request fails.
http.setMethod METHOD
http.setMethod "<METHOD>"
Set a http method. Use an uppercase string for GET|POST|PUT|DELETE|...