Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save treehousetim/040dfc8aab68d5549a9aeb2337a44f01 to your computer and use it in GitHub Desktop.

Select an option

Save treehousetim/040dfc8aab68d5549a9aeb2337a44f01 to your computer and use it in GitHub Desktop.
JSON Poster
#!/bin/bash
# Function to display usage
usage() {
echo "Usage: $0 [--json-file=<path_to_json_file> | --listen] --url=<target_url> [--striptags=0|1]"
echo "Examples:"
echo " $0 --json-file=./test.json --url=http://192.168.4.22/api/v4/member/webhook/"
echo " $0 --listen --url=http://192.168.4.22/api/v4/member/webhook/ --striptags=0"
echo "Note: HTML tags are stripped from response by default (--striptags=1) using PHP's strip_tags. Use --striptags=0 to disable."
exit 1
}
# Parse command line arguments
JSON_FILE=""
URL=""
LISTEN=false
STRIP_TAGS=true # Default to true (strip tags enabled)
for arg in "$@"; do
case $arg in
--json-file=*)
JSON_FILE="${arg#*=}"
shift
;;
--url=*)
URL="${arg#*=}"
shift
;;
--listen)
LISTEN=true
shift
;;
--striptags=0)
STRIP_TAGS=false
shift
;;
--striptags=1)
STRIP_TAGS=true
shift
;;
*)
usage
;;
esac
done
# Validate inputs
if [[ -z "$URL" ]]; then
usage
fi
if [[ "$LISTEN" == true && -n "$JSON_FILE" ]]; then
echo "Error: Cannot use --listen and --json-file together"
usage
fi
if [[ "$LISTEN" == false && -z "$JSON_FILE" ]]; then
echo "Error: Must provide either --json-file or --listen"
usage
fi
# Check if PHP is installed
if ! command -v php >/dev/null 2>&1; then
echo "Error: PHP is required for HTML tag stripping. Please install PHP."
exit 1
fi
# Function to strip HTML tags using PHP's strip_tags
strip_html_tags() {
local input="$1"
# Create a temporary PHP script to process the input
TEMP_PHP=$(mktemp)
cat <<EOF > "$TEMP_PHP"
<?php
\$input = file_get_contents('php://stdin');
\$stripped = strip_tags(\$input);
echo \$stripped;
?>
EOF
# Pipe the input to PHP and clean up
echo "$input" | php "$TEMP_PHP" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' | sed '/^[[:space:]]*$/d'
rm -f "$TEMP_PHP"
}
# Function to send POST request
send_request() {
local JSON_CONTENT="$1"
# Get current timestamp
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
echo "=== Request Details ==="
echo "Timestamp: $TIMESTAMP"
echo "URL: $URL"
if [[ "$LISTEN" == true ]]; then
echo "JSON Source: Pasted input"
else
echo "JSON File: $JSON_FILE"
fi
echo "JSON Content:"
echo "$JSON_CONTENT" | jq '.' 2>/dev/null || echo "$JSON_CONTENT"
echo ""
# Send POST request using curl and capture response
echo "=== Sending Request ==="
RESPONSE=$(curl -s -w "\n%{http_code}\n" \
-H "Content-Type: application/json" \
-d "$JSON_CONTENT" \
"$URL" 2>&1)
# Extract HTTP status code (last line of curl output)
HTTP_STATUS=$(echo "$RESPONSE" | tail -n1)
# Extract response body (everything except last line)
RESPONSE_BODY=$(echo "$RESPONSE" | sed '$d')
# Strip HTML tags if requested
if [[ "$STRIP_TAGS" == true ]]; then
RESPONSE_BODY=$(strip_html_tags "$RESPONSE_BODY")
fi
echo "=== Response Details ==="
echo "Timestamp: $TIMESTAMP"
echo "HTTP Status: $HTTP_STATUS"
echo "Response Body:"
echo "$RESPONSE_BODY" | jq '.' 2>/dev/null || echo "$RESPONSE_BODY"
echo ""
# Check if request was successful
if [[ "$HTTP_STATUS" -ge 200 && "$HTTP_STATUS" -lt 300 ]]; then
echo "Request completed successfully"
else
echo "Request failed with status code: $HTTP_STATUS"
fi
}
# Handle JSON file mode (single execution)
if [[ "$LISTEN" == false ]]; then
if [[ ! -f "$JSON_FILE" ]]; then
echo "Error: JSON file '$JSON_FILE' does not exist"
exit 1
fi
JSON_CONTENT=$(cat "$JSON_FILE")
send_request "$JSON_CONTENT"
exit 0
fi
# Handle listen mode (loop for multiple pastes)
echo "Entering listen mode. Paste JSON content (press Ctrl+D to submit, 'quit' to exit, Ctrl+C to force quit)."
while true; do
echo "Paste your JSON content (press Ctrl+D when done):"
JSON_CONTENT=$(cat)
if [[ -z "$JSON_CONTENT" ]]; then
echo "No JSON content provided. Enter 'quit' to exit or paste valid JSON."
read -p "Continue? (Enter to paste again, 'quit' to exit): " choice
if [[ "$choice" == "quit" ]]; then
echo "Exiting."
exit 0
fi
continue
fi
send_request "$JSON_CONTENT"
echo ""
read -p "Send another request? (Enter to paste again, 'quit' to exit): " choice
if [[ "$choice" == "quit" ]]; then
echo "Exiting."
exit 0
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment