Compare commits

..

No commits in common. "master" and "2.0.1" have entirely different histories.

6 changed files with 25 additions and 128 deletions

3
.gitignore vendored
View file

@ -1,7 +1,4 @@
curl/*
!curl/.gitkeep
saved/*
!saved/.gitkeep
disable_peer_validation

View file

@ -1,4 +1,4 @@
![curl-screenshot](https://codeberg.org/attachments/9bdd8005-5eea-4add-89c3-8efc61d06b6f)
![screenshot](https://codeberg.org/attachments/1cae98c6-0873-41fd-aad0-d33bf495fc50)
![license](https://licensebuttons.net/p/zero/1.0/88x31.png)
@ -76,13 +76,3 @@ Run the `curl.sh` file from your shell and pass it two parametes for URL and req
```sh
./curl.sh https://example.com GET
```
# Save and load configurations
The `save.sh` script allows you to save and load configurations so you can easily switch between them.
Command|Description
--|--
`./save.sh save <name>`|Save the current configuration where `<name>` is the name of this configuration
`./save.sh move <name>`|Move the current configuration where `<name>` is the name of this configuration. A new empty configuration will be created.
`./save.sh load <name>`|Load a saved configuration where `<name>` is the name of the target configuration. The current configuration will be saved as `._backup`.
`./save.sh list`|List all saved configurations

52
curl.sh
View file

@ -1,11 +1,14 @@
#!/bin/bash
# Check if the correct number of arguments is provided
if [ -z "${1:-}" ]; then
echo "Usage: $0 <url> [request_method]"
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"
@ -16,47 +19,39 @@ RESP_HEADERS_FILE="curl/resp_headers.txt"
RESP_BODY_RAW_FILE="curl/resp_body.txt"
RESP_BODY_JSON_FILE="curl/resp_body.json"
url="$1"
method="${2:-GET}"
# Append url search parameters
# 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/&$//')
if [ -n "$params" ]; then
url="${url}?${params}"
fi
PARAMS=$(tr '\n' '&' < "$REQ_PARAMS_FILE" | sed 's/&$//')
URL="${URL}?${PARAMS}"
fi
# Prepare curl
curl_cmd="curl -s"
CURL_CMD="curl -s"
# Check if SSL peer validation should be disabled
if [ -f $DISABLE_SSL_FILE ]; then
curl_cmd="$curl_cmd -k"
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\""
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\""
CURL_CMD="$CURL_CMD -X $METHOD \"$URL\""
# If the method is not GET, include the payload
if [ $method != "GET" ]; then
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'"
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
@ -64,14 +59,13 @@ if [ $method != "GET" ]; then
fi
# 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"
# Dispatch request
echo -e "\e[0;92mRequest >\e[0m ${method} ${url}"
eval $curl_cmd
# 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
# Print the response code
echo -e -n "\e[0;94mResponse <\e[0m "; head -n 1 $RESP_HEADERS_FILE

View file

@ -9,5 +9,3 @@ echo "" > resp_headers.txt
echo "{}" > req_body.json
echo "{}" > resp_body.json
echo "{}" > req_headers.json
echo "New configuration created"

82
save.sh
View file

@ -1,82 +0,0 @@
#!/bin/bash
SAVE_DIR="saved"
main () {
case "$1" in
# Save current config to a named directory
"save")
if [ "$#" -ne 2 ]; then
echo -e "\e[0;91mExpected: ./save.sh save <name>\e[0m"
return 1
fi
local dir="$SAVE_DIR/$2"
mkdir $dir 2>/dev/null
shopt -s nullglob
for file in "curl/req_"*; do
cp -- "$file" $dir
done
shopt -u nullglob
echo "Saved current configuration to: $2"
;;
# Move current config to a named directory
"move")
if [ "$#" -ne 2 ]; then
echo -e "\e[0;91mExpected: ./save.sh move <name>\e[0m"
return 1
fi
# Save config to destination
./save.sh save $2
shopt -s nullglob
for file in "curl/req_"*; do
rm -- "$file"
done
shopt -u nullglob
# Create new empty config
./init.sh
;;
# Load saved config by name from directory
"load")
local dir="$SAVE_DIR/$2"
if [ ! -d $dir ]; then
echo -e "\e[0;91mNo save with name '$2' found\e[0m"
exit 1
fi
# Move existing config to backup directory
./save.sh move ._backup
shopt -s nullglob
for file in "$dir/req_"*; do
cp -- "$file" curl/
done
shopt -u nullglob
echo "Loaded configuration: $2"
;;
"list")
ls -l $SAVE_DIR
;;
*)
echo -e "\e[0;91mExpected: ./save.sh (save|move|load|list)\e[0m"
;;
esac
}
main $1 $2

View file