webhook/docs/PreHook-Command.md
Adnan Hajdarevic 794a16ee13 wip
2020-11-19 22:19:48 +01:00

1.9 KiB

Pre-hook command

To the STDIN of the pre-hook command, webhook will pass a JSON string representation of an object with the following properties:

  • hookID - ID of the hook that got matched
  • method - HTTP(s) method used by the client (i.e. GET, POST, etc...)
  • URI - URI which client requested
  • host - value of the Host header sent by the client
  • remoteAddr - client's IP address and port in the IP:PORT format
  • query - object with query parameters and their respective values
  • headers - object with headers and their respective values
  • base64EncodedBody - base64 encoded request body

Please note! Output of this command MUST be valid JSON string which will be parsed by the webhook and accessible using the pre-hook as source when referencing values.

Important! Any errors encountered while trying to execute the pre-hook command will prevent the hook from triggering!

Examples

Please note: Following examples use shell scripts as pre-hook commands, but it is possible to use ruby, python, or anything else you like, as long as it outputs a valid JSON string as the result.

Make sure you have the jq command available, as we're using it to parse the JSON in the pre-hook script.

Getting the IP address of the requester

script.sh
#!/bin/bash

ip=$1

echo $ip >> ips.txt
prehook.sh
#!/bin/bash

context=$(cat)
ip=`echo $context | jq -r '.remoteAddr' | cut -d ':' -f 1`

echo "{\"ip\": \"$ip\"}"
hooks.json
[
    {
        "id": "log-ip",
        "pre-hook-command": "/home/example/prehook.sh",
        "execute-command": "/home/example/script.sh",
        "pass-arguments-to-command": [
            { "source": "pre-hook", "name": "ip" }
        ]
    }
]