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

update readme and docs

parent 57c9e2d0
No related branches found
No related tags found
1 merge request!2Extend authorization
# REST API CLIENT
# Bash REST API client
This is a bash solution to script REST API calls.
......@@ -12,16 +12,21 @@ The specialties of this component are
* 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
Source: <https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client>
👤 Author: Axel Hahn; Institute for Medical Education; University of Bern \
📄 Source: <https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client> \
📜 License: GNU GPL 3.0 \
📗 Docs: <https://os-docs.iml.unibe.ch/bash-rest-api-client/>
License: GNU GPL 3
## Requirements
```text
http.init
http.makeRequest 'http://www.example.com/'
```
* Bash as shell (runs on Linux - or with Cygwin or other Bash implementations on MS Windows too.)
* Curl must be in the path
* optional: sha1sum (for export function with autogenerated filenames only)
This stores the response in a variable and has no output.
Now you can get its data, eg
---
[Installation](docs/10_Installation.md) | [Usage](docs/20_Usage.md) | [Examples](docs/30_Examples.md)
\ No newline at end of file
* http.getStatuscode - This returns the Http status code
* http.getResponseHeader - print Http response header
* http.getResponseData - get data from curl
* http.getResponse - get response body
## Introduction
### Description
This is a bash solution to script REST API calls.
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
* 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
### Requirements
* Bash as shell (runs on Linux - or with Cygwin or other Bash implementations on MS Windows too.)
* Curl must be in the path
* optional: sha1sum (for export function with autogenerated filenames only)
# Download
## Installation
### Get all files
Download the archive i.e. as zip:
https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client/-/archive/master/bash-rest-api-client-master.zip
(see other formats at the download button on https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client)
# OR: Git clone
#### Git clone
```sh
git clone https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client.git
```
# Copy somewhere
#### Download
Download the archive i.e. as zip:
Copy the file `rest-api-client.sh` anywhere you want.
<https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client/-/archive/master/bash-rest-api-client-master.zip>
(see other formats at the download button on <https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client>)
#### Copy shell script somewhere
Copy the file `rest-api-client.sh` anywhere you need it.
If you need it in a single script then copy it to its directory. If you need it more often then copy it to a folter of your PATH environment.
### Get the script only
`wget https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client/-/raw/master/rest-api-client.sh`
......@@ -7,7 +7,7 @@ The "methods" start with "http" dot "[method]".
You should start with *http.help* to get an overwiew.
```sh
```text
#
# step one: source the shell script
#
......@@ -18,6 +18,15 @@ $ . ./rest-api-client.sh
#
$ http.help
Bash REST API client v0.8
This is a bash solution to script REST API calls.
Source:https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client
License: GNU GPL 3
INSTRUCTION:
- Source the file once
......@@ -31,11 +40,19 @@ INSTRUCTION:
Enable or disable debugging infos during processing. It is written
to STDERR.
- initialize a request
setAccept ACCEPT
Set authentication with user and password for basic auth
Default: application/json
setAuth AUTH:PASSWORD
set authentication
Set authentication with user and password for basic auth
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
http.setBody DATA
set a body for POST/ PUT requests.
......@@ -57,6 +74,10 @@ INSTRUCTION:
Set a relative url for a request.
This requires to use http.setBaseUrl before.
http.addHeader HEADER_LINE
Add a header line to the request.
This command can be repeated multiple times.
- caching functions
http.setCacheTtl SECONDS
......@@ -103,7 +124,7 @@ INSTRUCTION:
Get the http status as string Ok|Redirect|Error
http.getStatuscode
Get the http status code of a request as integer
Get the http status code of a request as 3 digit integer
http.isOk
Check if the http response code is a 2xx
......
# Preparation
## Preparation
A required step is sourcing the script to make the http.* function available in the current shell.
......@@ -9,7 +9,7 @@ A required step is sourcing the script to make the http.* function available in
Then you can follow the examples.
# A first example
## A first example
This is quick introduction: we make a request and have a look to a few results.
......@@ -67,7 +67,7 @@ url_effective:http://www.iml.unibe.ch/
```
# Icinga2 API
## 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.
......@@ -113,3 +113,32 @@ if [ $? -ne 0 ]; then
fi
echo "OK, ${myHost} was found."
```
## Authentication with JWT token
The snippet shows how to fetch a token first that will be added to be used in all following requests.
```sh
# ---------- 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}"
# ---------- Requests to app or api
http.makeRequest GET "api"
http.makeRequest GET "app"
```
# Description
This is a bash solution to script REST API calls.
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
* 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
Source: https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client
License: GNU GPL 3
# Requirements
* Bash as shell (runs on Linux - or with Cygwin or other Bash implementations on MS Windows too.)
* Curl must be in the path
* optional: sha1sum (for export function with autogenerated filenames only)
<html>
<div class="hero">
<h2>Bash REST API client</h2>
</div>
</html>
👤 Author: Axel Hahn; Institute for Medical Education; University of Bern \
📄 Source: <https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client> \
📜 License: GNU GPL 3.0 \
📗 Docs: <https://os-docs.iml.unibe.ch/bash-rest-api-client/>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment