This Bash script is a versatile tool for making HTTP/HTTPS requests. It aims to provide a simple yet flexible way to interact with web services directly from your terminal. In addition, it is designed to function as a library, offering a function for making HTTP/HTTPS requests. This function allows you to save each component of the request - response headers, response body, and response code - to a variable.
- Supports multiple tools for making requests (curl, wget, openssl s_client, nc)
- Autodetects what tools are available and uses the appropriate one for the request
- Customizable environment variables
- Basic error handling and logging
- Extract components from URLs
Clone the repository or download the bash_requests.sh
file and give it executable permissions:
chmod +x bash_requests.sh
Run the script from the command line as shown below:
./bash_requests.sh [OPTIONS] <URL> "<HEADER>" "<HEADER>"
- -h, --help: Display the help screen.
- -d, --debug: Enable debug output.
- -t, --tool : Choose the tool to be used. Available tools are: curl, wget, openssl, and nc.
- "": Add headers for the request. You can add multiple headers; just enclose each in quotes.
- : The URL for the request. No need for quotes here, but you can use them if your URL is complex.
- Making a request with debug output and a specified header:
./bash_requests.sh -d https://api.twitter.com/2/tweets/123456789 "Authorization: Bearer TOKEN"
- Using a specific tool (curl in this example) with debug output and a specified user-agent:
./bash_requests.sh -t curl -d https://api.twitter.com/2/tweets/123456789 "User-Agent: MyUserAgent"
To leverage the power of bash_requests.sh
directly in your script, you can source the file and utilize the request()
function:
source bash_requests.sh
request "$url" "$header1" "$header2" "$header3"
The request()
function streamlines the process of making HTTP/HTTPS requests. Here's how it works:
-
Parameters:
URL
: The first argument is always the URL for the request.HEADERS
: All subsequent arguments are treated as headers. Multiple headers can be passed, and they should each be enclosed in quotes.
-
Global Variables Set After Execution:
RESPONSE_CODE
: Holds the HTTP response code from the server.RESPONSE_BODY
: Contains the body content of the HTTP response.RESPONSE_HEADERS
: Captures the headers returned in the HTTP response.
Important: After each request, these global variables are overwritten. If you need their values for further processing, ensure you capture and store them before initiating a new request.
- Making a simple GET request to a website:
source bash_requests.sh
request "https://www.example.com"
echo $RESPONSE_BODY
- Making a request with multiple headers:
source bash_requests.sh
request "https://api.example.com/data" "Authorization: Bearer TOKEN" "User-Agent: MyUserAgent"
echo $RESPONSE_HEADERS
- Making a request and capturing the response code:
source bash_requests.sh
request "https://api.example.com/data"
echo "Received Response Code: $RESPONSE_CODE"
The script uses the following environment variables:
DEFAULT_USER_AGENT
: Default user agent if none is providedLOG_FILE
: Log file for storing errors
The script logs any request that doesn't return a status code of 200 or 203. If such a non-compliant status code is received, the details are captured and saved to the specified log file.
The included unit tests can be used to validate any changes or improvements are non-breaking.
Logs are stored in bash_requests_error.log
by default.
Feel free to open an issue or submit a pull request if you find any bugs or have suggestions for improvements. Include the testing library output in your pull request.
MIT License
- How do I change the default log file?
- Modify the
LOG_FILE
environment variable.
- Modify the