Compare commits

..

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

3 changed files with 18 additions and 37 deletions

2
.gitignore vendored
View file

@ -1,4 +1,4 @@
key.txt
params.txt params.txt
headers.json
payload.json payload.json
disable_peer_validation disable_peer_validation

View file

@ -1,39 +1,22 @@
![license](https://licensebuttons.net/p/zero/1.0/88x31.png) ![license](https://licensebuttons.net/p/zero/1.0/88x31.png)
# Simple curl bash wrapper # A simple curl wrapper written in bash that can be used to make HTTP requests to API endpoints.
<img src="https://blob.vlw.se/01/0199056d-d6fc-748b-adbb-269c8c5237ab.png"> ![screenshot](https://href.vlw.se/0194ea19-faa6-77b5-8fe1-b1459a12ea84)
--- I use this script with windows set up like this in code-server.
**This is a very simple wrapper for curl that I use with VSCode (code-server) to make HTTP requests - like Postman but super simple.**
VSCode is not required to run this script, I use it as a GUI.
# Files # Files
This script uses separate files for various request options. Create these files in the same directory as `curl.sh`. This script uses separate files for various request options. Create these files in the same directory as `curl.sh`.
## `params.txt` ## `params.txt`
URL search parameters. (Leading "?" is optional) URL search parameters.
```
foo=bar&hello=world
```
## `payload.json` ## `paylaod.json`
JSON request body that will be sent with all requests (except `GET`). JSON request body that will be sent with all requests (except `GET`).
```json
{
"request_body_parameter": "request_body_value"
}
```
## `headers.json` ## `key.txt`
Key-value JSON object of optional request headers. [HTTP Authentication Bearer Token](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication)
```json
{
"X-Header-Name": "Header value"
}
```
## `disable_peer_validation` ## `disable_peer_validation`
Optional empty file that when present will disable SSL peer validation - for self-signed certificates etc. Optional empty file that when present will disable SSL peer validation - for self-signed certificates etc.

22
curl.sh
View file

@ -8,8 +8,8 @@ fi
URL="$1" URL="$1"
METHOD="$2" METHOD="$2"
KEY_FILE="key.txt"
PARAMS_FILE="params.txt" PARAMS_FILE="params.txt"
HEADERS_FILE="headers.json"
PAYLOAD_FILE="payload.json" PAYLOAD_FILE="payload.json"
DISABLE_SSL_FILE="disable_peer_validation" DISABLE_SSL_FILE="disable_peer_validation"
@ -19,24 +19,22 @@ if [ -f $PARAMS_FILE ]; then
URL="${URL}?${PARAMS}" URL="${URL}?${PARAMS}"
fi fi
# Check if the key file exists and read the Bearer token
if [ -f $KEY_FILE ]; then
BEARER_TOKEN=$(<"$KEY_FILE")
else
echo "Error: Bearer token file '$KEY_FILE' not found."
exit 1
fi
# Prepare the curl command # Prepare the curl command
CURL_CMD="curl -s" CURL_CMD="curl -s -H \"Authorization: Bearer ${BEARER_TOKEN}\""
# 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
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 # Add the request method and URL to the curl command
CURL_CMD="$CURL_CMD -X $METHOD \"$URL\"" CURL_CMD="$CURL_CMD -X $METHOD \"$URL\""