Prompt Detail:
To check if an API is up or not and send the result via email in tabular format, you can use the curl
command to make a request to the API and check the HTTP status code. Here's an example script:
bash#!/bin/bash
# Set API endpoint and recipient email address
endpoint="https://example.com/api"
recipient="[email protected]"
# Make API request and check HTTP status code
response=$(curl -s -o /dev/null -w "%{http_code}" "$endpoint")
if [ $response -eq 200 ]; then
message="<tr><td>$endpoint</td><td>OK</td></tr>"
else
message="<tr><td>$endpoint</td><td>FAILED</td></tr>"
fi
# Generate HTML table
table="<table><tr><th>API Endpoint</th><th>Status</th></tr>$message</table>"
# Send email with report
echo "$table" | mail -a "Content-Type: text/html" -s "API Health Report" "$recipient"
This script uses the curl
command to make a request to the API endpoint specified in the endpoint
variable. The -s
option suppresses the progress meter and the -o /dev/null
option sends the response to /dev/null
so that only the HTTP status code is displayed. If the status code is 200
, the API is considered to be up and the message in the HTML table is set to "OK". Otherwise, the message is set to "FAILED".
The HTML table is generated in the same way as the previous example. The email subject is set to "API Health Report" and the recipient email address is set in the recipient
variable.
You can customize this script to check multiple API endpoints and include additional information in the email report, such as the response time or the response body.