mirror of
https://codeberg.org/vlw/curl.git
synced 2026-04-13 02:39:38 +02:00
Compare commits
8 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 64db42527b | |||
| 29090ad555 | |||
| 832bea4252 | |||
| 7b70a6e51d | |||
| e015174467 | |||
| 328cebc703 | |||
| a42be0792c | |||
| dfd0664255 |
3 changed files with 32 additions and 24 deletions
|
|
@ -1,4 +1,4 @@
|
||||||

|

|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
|
|
||||||
52
curl.sh
52
curl.sh
|
|
@ -1,14 +1,11 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# Check if the correct number of arguments is provided
|
# Check if the correct number of arguments is provided
|
||||||
if [ "$#" -ne 2 ]; then
|
if [ -z "${1:-}" ]; then
|
||||||
echo "Usage: $0 <url> <request_method>"
|
echo "Usage: $0 <url> [request_method]"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
URL="$1"
|
|
||||||
METHOD="$2"
|
|
||||||
|
|
||||||
DISABLE_SSL_FILE="disable_peer_validation"
|
DISABLE_SSL_FILE="disable_peer_validation"
|
||||||
|
|
||||||
REQ_BODY_FILE="curl/req_body.json"
|
REQ_BODY_FILE="curl/req_body.json"
|
||||||
|
|
@ -19,39 +16,47 @@ RESP_HEADERS_FILE="curl/resp_headers.txt"
|
||||||
RESP_BODY_RAW_FILE="curl/resp_body.txt"
|
RESP_BODY_RAW_FILE="curl/resp_body.txt"
|
||||||
RESP_BODY_JSON_FILE="curl/resp_body.json"
|
RESP_BODY_JSON_FILE="curl/resp_body.json"
|
||||||
|
|
||||||
# Append URL search parameters
|
url="$1"
|
||||||
|
method="${2:-GET}"
|
||||||
|
|
||||||
|
# Append url search parameters
|
||||||
if [ -f $REQ_PARAMS_FILE ]; then
|
if [ -f $REQ_PARAMS_FILE ]; then
|
||||||
# Parse each newline as a separate parameter joined by a "&"
|
# Parse each newline as a separate parameter joined by a "&"
|
||||||
PARAMS=$(tr '\n' '&' < "$REQ_PARAMS_FILE" | sed 's/&$//')
|
params=$(tr '\n' '&' < "$REQ_PARAMS_FILE" | sed 's/&$//')
|
||||||
URL="${URL}?${PARAMS}"
|
|
||||||
|
if [ -n "$params" ]; then
|
||||||
|
url="${url}?${params}"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Prepare curl
|
# Prepare curl
|
||||||
CURL_CMD="curl -s"
|
curl_cmd="curl -s"
|
||||||
|
|
||||||
# Check if SSL peer validation should be disabled
|
# Check if SSL peer validation should be disabled
|
||||||
if [ -f $DISABLE_SSL_FILE ]; then
|
if [ -f $DISABLE_SSL_FILE ]; then
|
||||||
CURL_CMD="$CURL_CMD -k"
|
curl_cmd="$curl_cmd -k"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Add headers from headers.json
|
# Add headers from headers.json
|
||||||
if [ -f $REQ_HEADERS_FILE ]; then
|
if [ -f $REQ_HEADERS_FILE ]; then
|
||||||
# Read headers from the JSON file
|
# Read headers from the JSON file
|
||||||
while IFS= read -r line; do
|
while IFS= read -r line; do
|
||||||
HEADER_NAME=$(echo "$line" | jq -r '.[keys[0]]')
|
header_name=$(echo "$line" | jq -r '.[keys[0]]')
|
||||||
HEADER_VALUE=$(echo "$line" | jq -r '.[keys[1]]')
|
header_value=$(echo "$line" | jq -r '.[keys[1]]')
|
||||||
CURL_CMD="$CURL_CMD -H \"$HEADER_NAME: $HEADER_VALUE\""
|
|
||||||
|
curl_cmd="$curl_cmd -H \"$header_name: $header_value\""
|
||||||
done < <(jq -c 'to_entries | .[]' "$REQ_HEADERS_FILE")
|
done < <(jq -c 'to_entries | .[]' "$REQ_HEADERS_FILE")
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Add the request method and URL to curl
|
# Add the request method and URL to curl
|
||||||
CURL_CMD="$CURL_CMD -X $METHOD \"$URL\""
|
curl_cmd="$curl_cmd -X $method \"$url\""
|
||||||
|
|
||||||
# If the method is not GET, include the payload
|
# If the method is not GET, include the payload
|
||||||
if [ $METHOD != "GET" ]; then
|
if [ $method != "GET" ]; then
|
||||||
if [ -f "$REQ_BODY_FILE" ]; then
|
if [ -f "$REQ_BODY_FILE" ]; then
|
||||||
PAYLOAD=$(<"$REQ_BODY_FILE")
|
payload=$(<"$REQ_BODY_FILE")
|
||||||
CURL_CMD="$CURL_CMD -H \"Content-Type: application/json\" -d '$PAYLOAD'"
|
|
||||||
|
curl_cmd="$curl_cmd -H \"Content-Type: application/json\" -d '$payload'"
|
||||||
else
|
else
|
||||||
echo "Error: Payload file '${REQ_BODY_FILE}' not found."
|
echo "Error: Payload file '${REQ_BODY_FILE}' not found."
|
||||||
exit 1
|
exit 1
|
||||||
|
|
@ -59,13 +64,14 @@ if [ $METHOD != "GET" ]; then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Save the response headers and body
|
# Save the response headers and body
|
||||||
CURL_CMD="$CURL_CMD -o $RESP_BODY_RAW_FILE -D $RESP_HEADERS_FILE"
|
curl_cmd="$curl_cmd -o $RESP_BODY_RAW_FILE -D $RESP_HEADERS_FILE"
|
||||||
|
|
||||||
# Execute curl
|
# Dispatch request
|
||||||
eval "clear"
|
echo -e "\e[0;92mRequest >\e[0m ${method} ${url}"
|
||||||
|
eval $curl_cmd
|
||||||
echo -e "${METHOD} ${URL}"
|
|
||||||
eval $CURL_CMD
|
|
||||||
|
|
||||||
# Copy the raw respone body to a pretty-printed JSON file
|
# Copy the raw respone body to a pretty-printed JSON file
|
||||||
jq . $RESP_BODY_RAW_FILE > $RESP_BODY_JSON_FILE
|
jq . $RESP_BODY_RAW_FILE > $RESP_BODY_JSON_FILE
|
||||||
|
|
||||||
|
# Print the response code
|
||||||
|
echo -e -n "\e[0;94mResponse <\e[0m "; head -n 1 $RESP_HEADERS_FILE
|
||||||
|
|
|
||||||
2
init.sh
2
init.sh
|
|
@ -9,3 +9,5 @@ echo "" > resp_headers.txt
|
||||||
echo "{}" > req_body.json
|
echo "{}" > req_body.json
|
||||||
echo "{}" > resp_body.json
|
echo "{}" > resp_body.json
|
||||||
echo "{}" > req_headers.json
|
echo "{}" > req_headers.json
|
||||||
|
|
||||||
|
echo "New configuration created"
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue