mirror of
https://codeberg.org/vlw/curl.git
synced 2026-04-13 10:49:39 +02:00
71 lines
1.9 KiB
Bash
Executable file
71 lines
1.9 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"
|
|
|
|
DISABLE_SSL_FILE="disable_peer_validation"
|
|
|
|
REQ_BODY_FILE="curl/req_body.json"
|
|
REQ_PARAMS_FILE="curl/req_params.txt"
|
|
REQ_HEADERS_FILE="curl/req_headers.json"
|
|
|
|
RESP_HEADERS_FILE="curl/resp_headers.txt"
|
|
RESP_BODY_RAW_FILE="curl/resp_body.txt"
|
|
RESP_BODY_JSON_FILE="curl/resp_body.json"
|
|
|
|
# Append URL search parameters
|
|
if [ -f $REQ_PARAMS_FILE ]; then
|
|
# Parse each newline as a separate parameter joined by a "&"
|
|
PARAMS=$(tr '\n' '&' < "$REQ_PARAMS_FILE" | sed 's/&$//')
|
|
URL="${URL}?${PARAMS}"
|
|
fi
|
|
|
|
# Prepare curl
|
|
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 $REQ_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 | .[]' "$REQ_HEADERS_FILE")
|
|
fi
|
|
|
|
# Add the request method and URL to curl
|
|
CURL_CMD="$CURL_CMD -X $METHOD \"$URL\""
|
|
|
|
# If the method is not GET, include the payload
|
|
if [ $METHOD != "GET" ]; then
|
|
if [ -f "$REQ_BODY_FILE" ]; then
|
|
PAYLOAD=$(<"$REQ_BODY_FILE")
|
|
CURL_CMD="$CURL_CMD -H \"Content-Type: application/json\" -d '$PAYLOAD'"
|
|
else
|
|
echo "Error: Payload file '${REQ_BODY_FILE}' not found."
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
# Save the response headers and body
|
|
CURL_CMD="$CURL_CMD -o $RESP_BODY_RAW_FILE -D $RESP_HEADERS_FILE"
|
|
|
|
# Execute curl
|
|
eval "clear"
|
|
|
|
echo -e "${METHOD} ${URL}"
|
|
eval $CURL_CMD
|
|
|
|
# Copy the raw respone body to a pretty-printed JSON file
|
|
jq . $RESP_BODY_RAW_FILE > $RESP_BODY_JSON_FILE
|