feat: save output of response headers and body to files (#3)

In this PR we add a new directly to store the files used with curl. We also put the response body and headers into files now instead of echoing them to the console. This makes them easier to read, and we can pretty-print JSON response bodies with proper syntax highlighting (and more). Since we have a few files now, I've also added an "init.sh" file that can be used to create the necessary files on first load.

Reviewed-on: https://codeberg.org/vlw/curl/pulls/3
Co-authored-by: vlw <victor@vlw.se>
Co-committed-by: vlw <victor@vlw.se>
This commit is contained in:
Victor Westerlund 2026-03-14 10:40:50 +01:00 committed by Victor Westerlund
parent 73bf60d031
commit 31d79e0c0d
5 changed files with 91 additions and 46 deletions

8
.gitignore vendored
View file

@ -1,4 +1,4 @@
params.txt
headers.json
payload.json
disable_peer_validation
curl/*
!curl/.gitkeep
disable_peer_validation

View file

@ -1,33 +1,58 @@
![screenshot](https://codeberg.org/attachments/1cae98c6-0873-41fd-aad0-d33bf495fc50)
![license](https://licensebuttons.net/p/zero/1.0/88x31.png)
# Simple curl bash wrapper
<img src="https://blob.vlw.se/01/0199056d-d6fc-748b-adbb-269c8c5237ab.png">
This is a wrapper for curl written in bash designed to be used along with Visual Studio Code.
---
> Mom, can we get Postman?
>
> We've got Postman at home!
>
> [\- Source](https://knowyourmeme.com/memes/we-have-food-at-home)
**This is a very simple wrapper for curl that I use with VSCode (code-server) to make HTTP requests - like Postman but super simple.**
[Here is a simpler version of this script that prints the response body directly to the console](/vlw/curl/src/tag/1.1.0)
VSCode is not required to run this script, I use it as a GUI.
# Get started
Clone this repo
```sh
git clone https://codeberg.org/vlw/curl --depth 1
```
This script uses separate files in the `curl/` directory for various request options. You can generate these files on first load by running the init script.
```sh
./init.sh
```
> [!Note]
> If you're using this with VSCode, drag each file into the workspace area where you want it to display. You of course don't have to follow the layout in the screenshot above.
# Files
This script uses separate files for various request options. Create these files in the same directory as `curl.sh`.
This script reads or writes values in these files to construct a request or show a response from a request.
## `params.txt`
URL search parameters. (Leading "?" is optional)
```
foo=bar&hello=world
## Disable SSL peer validation
You can disable SSL peer validation (for self-signed certificates etc.) by creating an empty file `disable_peer_validation` in the base directory of this repo.
```sh
touch disable_peer_validation
```
## `payload.json`
JSON request body that will be sent with all requests (except `GET`).
## Request
### `curl/req_params.txt`
URL search parameters. Each newline is treated as a separate search parameter.
```
foo=bar
fizz=buzz
```
### `curl/req_body.json`
JSON request body that will be sent with all requests (except `GET`). Leave empty to send nothing.
```json
{
"request_body_parameter": "request_body_value"
}
```
## `headers.json`
### `curl/req_headers.json`
Key-value JSON object of optional request headers.
```json
{
@ -35,23 +60,19 @@ Key-value JSON object of optional request headers.
}
```
## `disable_peer_validation`
Optional empty file that when present will disable SSL peer validation - for self-signed certificates etc.
## Response
### `curl/resp_headers.txt`
A raw output of the response headers from the last request
### `curl/resp_body.txt`
The raw response body received from the last request
### `curl/resp_body.json`
The raw response body parsed into- and automatically pretty-printed JSON
# Make a request
Run the `curl.sh` file from your shell and pass it two parametes for URL and request method.
```sh
./curl.sh https://example.com GET
```
The response body will be printed to stdout.
*Example*
```
vlw@example:/curl$ ./curl.sh https://example.com GET
GET https://example.com?foo=bar
"This is an example response body in plaintext"
vlw@example:/curl$
```

45
curl.sh
View file

@ -8,18 +8,25 @@ fi
URL="$1"
METHOD="$2"
PARAMS_FILE="params.txt"
HEADERS_FILE="headers.json"
PAYLOAD_FILE="payload.json"
DISABLE_SSL_FILE="disable_peer_validation"
# Append URL search parameters from params.txt
if [ -f $PARAMS_FILE ]; then
PARAMS=$(<params.txt)
REQ_BODY_FILE="curl/req_body.json"
REQ_PARAMS_FILE="curl/req_params.txt"
REQ_HEADERS_FILE="curl/req_headers.json"
RESP_HEADERS_FILE="curl/resp_headers.txt"
RESP_BODY_RAW_FILE="curl/resp_body.txt"
RESP_BODY_JSON_FILE="curl/resp_body.json"
# 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/&$//')
URL="${URL}?${PARAMS}"
fi
# Prepare the curl command
# Prepare curl
CURL_CMD="curl -s"
# Check if SSL peer validation should be disabled
@ -28,31 +35,37 @@ if [ -f $DISABLE_SSL_FILE ]; then
fi
# Add headers from headers.json
if [ -f $HEADERS_FILE ]; then
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\""
done < <(jq -c 'to_entries | .[]' "$HEADERS_FILE")
done < <(jq -c 'to_entries | .[]' "$REQ_HEADERS_FILE")
fi
# Add the request method and URL to the curl command
# Add the request method and URL to curl
CURL_CMD="$CURL_CMD -X $METHOD \"$URL\""
# If the method is not GET, include the payload
if [ $METHOD != "GET" ]; then
if [ -f "$PAYLOAD_FILE" ]; then
PAYLOAD=$(<"$PAYLOAD_FILE")
if [ -f "$REQ_BODY_FILE" ]; then
PAYLOAD=$(<"$REQ_BODY_FILE")
CURL_CMD="$CURL_CMD -H \"Content-Type: application/json\" -d '$PAYLOAD'"
else
echo "Error: Payload file '${PAYLOAD_FILE}' not found."
echo "Error: Payload file '${REQ_BODY_FILE}' not found."
exit 1
fi
fi
# Execute the curl command
# Save the response headers and body
CURL_CMD="$CURL_CMD -o $RESP_BODY_RAW_FILE -D $RESP_HEADERS_FILE"
# Execute curl
eval "clear"
echo -e "${METHOD} ${URL}\n"
echo -e "${METHOD} ${URL}"
eval $CURL_CMD
echo -e ""
# Copy the raw respone body to a pretty-printed JSON file
jq . $RESP_BODY_RAW_FILE > $RESP_BODY_JSON_FILE

0
curl/.gitkeep Normal file
View file

11
init.sh Executable file
View file

@ -0,0 +1,11 @@
#!/bin/bash
cd curl
echo "" > resp_body.txt
echo "" > req_params.txt
echo "" > resp_headers.txt
echo "{}" > req_body.json
echo "{}" > resp_body.json
echo "{}" > req_headers.json