curl/curl.sh

58 lines
No EOL
1.4 KiB
Bash
Executable file

#!/bin/bash
# Check if the correct number of arguments is provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <url> <request_method>"
exit 1
fi
URL="$1"
METHOD="$2"
PARAMS_FILE="params.txt"
HEADERS_FILE="headers.json"
PAYLOAD_FILE="payload.json"
DISABLE_SSL_FILE="disable_peer_validation"
# Append URL search parameters from params.txt
if [ -f $PARAMS_FILE ]; then
PARAMS=$(<params.txt)
URL="${URL}?${PARAMS}"
fi
# Prepare the curl command
CURL_CMD="curl -s"
# Check if SSL peer validation should be disabled
if [ -f $DISABLE_SSL_FILE ]; then
CURL_CMD="$CURL_CMD -k"
fi
# Add headers from headers.json
if [ -f $HEADERS_FILE ]; then
# Read headers from the JSON file
while IFS= read -r line; do
HEADER_NAME=$(echo "$line" | jq -r '.[keys[0]]')
HEADER_VALUE=$(echo "$line" | jq -r '.[keys[1]]')
CURL_CMD="$CURL_CMD -H \"$HEADER_NAME: $HEADER_VALUE\""
done < <(jq -c 'to_entries | .[]' "$HEADERS_FILE")
fi
# Add the request method and URL to the curl command
CURL_CMD="$CURL_CMD -X $METHOD \"$URL\""
# If the method is not GET, include the payload
if [ $METHOD != "GET" ]; then
if [ -f "$PAYLOAD_FILE" ]; then
PAYLOAD=$(<"$PAYLOAD_FILE")
CURL_CMD="$CURL_CMD -H \"Content-Type: application/json\" -d '$PAYLOAD'"
else
echo "Error: Payload file '${PAYLOAD_FILE}' not found."
exit 1
fi
fi
# Execute the curl command
eval "clear"
echo -e "${METHOD} ${URL}\n"
eval $CURL_CMD
echo -e ""