Compare commits

..

1 commit

Author SHA1 Message Date
73bf60d031 feat: add headers.json for specifying optional request headers (#2)
This PR replaces `key.txt` with `headers.json` which allows for optional request headers to be set.

Reviewed-on: https://codeberg.org/vlw/curl/pulls/2
2025-09-01 15:29:13 +02:00
3 changed files with 37 additions and 18 deletions

2
.gitignore vendored
View file

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

View file

@ -1,22 +1,39 @@
![license](https://licensebuttons.net/p/zero/1.0/88x31.png)
# A simple curl wrapper written in bash that can be used to make HTTP requests to API endpoints.
# Simple curl bash wrapper
![screenshot](https://href.vlw.se/0194ea19-faa6-77b5-8fe1-b1459a12ea84)
<img src="https://blob.vlw.se/01/0199056d-d6fc-748b-adbb-269c8c5237ab.png">
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
This script uses separate files for various request options. Create these files in the same directory as `curl.sh`.
## `params.txt`
URL search parameters.
URL search parameters. (Leading "?" is optional)
```
foo=bar&hello=world
```
## `paylaod.json`
## `payload.json`
JSON request body that will be sent with all requests (except `GET`).
```json
{
"request_body_parameter": "request_body_value"
}
```
## `key.txt`
[HTTP Authentication Bearer Token](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication)
## `headers.json`
Key-value JSON object of optional request headers.
```json
{
"X-Header-Name": "Header value"
}
```
## `disable_peer_validation`
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"
METHOD="$2"
KEY_FILE="key.txt"
PARAMS_FILE="params.txt"
HEADERS_FILE="headers.json"
PAYLOAD_FILE="payload.json"
DISABLE_SSL_FILE="disable_peer_validation"
@ -19,22 +19,24 @@ if [ -f $PARAMS_FILE ]; then
URL="${URL}?${PARAMS}"
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
CURL_CMD="curl -s -H \"Authorization: Bearer ${BEARER_TOKEN}\""
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 $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
CURL_CMD="$CURL_CMD -X $METHOD \"$URL\""