mirror of
https://codeberg.org/vlw/curl.git
synced 2026-04-13 10:49:39 +02:00
Compare commits
No commits in common. "master" and "2.0.0" have entirely different histories.
6 changed files with 27 additions and 130 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -1,7 +1,4 @@
|
||||||
curl/*
|
curl/*
|
||||||
!curl/.gitkeep
|
!curl/.gitkeep
|
||||||
|
|
||||||
saved/*
|
|
||||||
!saved/.gitkeep
|
|
||||||
|
|
||||||
disable_peer_validation
|
disable_peer_validation
|
||||||
|
|
|
||||||
18
README.md
18
README.md
|
|
@ -1,13 +1,13 @@
|
||||||

|

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

|

|
||||||
|
|
||||||
|
|
||||||
This is a wrapper for curl written in bash designed to be used along with Visual Studio Code.
|
This is a wrapper for curl written in bash designed to be used along with Visual Studio Code.
|
||||||
|
|
||||||
> Mom, can we have Postman?
|
> Mom, can we get Postman?
|
||||||
>
|
>
|
||||||
> We have Postman at home!
|
> We've got Postman at home!
|
||||||
>
|
>
|
||||||
> [\- Source](https://knowyourmeme.com/memes/we-have-food-at-home)
|
> [\- Source](https://knowyourmeme.com/memes/we-have-food-at-home)
|
||||||
|
|
||||||
|
|
@ -75,14 +75,4 @@ Run the `curl.sh` file from your shell and pass it two parametes for URL and req
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
./curl.sh https://example.com GET
|
./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
52
curl.sh
|
|
@ -1,11 +1,14 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# Check if the correct number of arguments is provided
|
# Check if the correct number of arguments is provided
|
||||||
if [ -z "${1:-}" ]; then
|
if [ "$#" -ne 2 ]; 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"
|
||||||
|
|
@ -16,47 +19,39 @@ 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"
|
||||||
|
|
||||||
url="$1"
|
# Append URL search parameters
|
||||||
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
|
||||||
|
|
@ -64,14 +59,13 @@ 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"
|
||||||
|
|
||||||
# Dispatch request
|
# Execute curl
|
||||||
echo -e "\e[0;92mRequest >\e[0m ${method} ${url}"
|
eval "clear"
|
||||||
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,5 +9,3 @@ 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"
|
|
||||||
|
|
|
||||||
82
save.sh
82
save.sh
|
|
@ -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
|
|
||||||
Loading…
Add table
Reference in a new issue