From 73bf60d031fad0b1d187e1d1d9da43ed75a29750 Mon Sep 17 00:00:00 2001 From: Victor Westerlund Date: Mon, 1 Sep 2025 15:29:13 +0200 Subject: [PATCH] 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 --- .gitignore | 2 +- README.md | 31 ++++++++++++++++++++++++------- curl.sh | 22 ++++++++++++---------- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 7e3f9a9..252e539 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -key.txt params.txt +headers.json payload.json disable_peer_validation \ No newline at end of file diff --git a/README.md b/README.md index 878a961..72256ed 100644 --- a/README.md +++ b/README.md @@ -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) + -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. diff --git a/curl.sh b/curl.sh index 55378b3..db614c0 100755 --- a/curl.sh +++ b/curl.sh @@ -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\""