lib/post.sh

660 B · 31 lines · bash

1#!/usr/bin/env bash
2
3brt_post() {
4 local url="${1:-}"
5 if [[ -z "$url" ]]; then
6 brt_error "Usage: brt -post <url> [curl flags...]"
7 brt_info "Example: brt -post https://httpbin.org/post -H 'Content-Type: application/json' -d '{\"ok\":true}'"
8 exit 1
9 fi
10 shift
11
12 brt_require curl
13
14 local tmp stats
15 tmp=$(mktemp)
16 stats=$(mktemp)
17
18 if ! curl -sS -X POST -w "http_code:%{http_code}\nsize:%{size_download}\ntime:%{time_total}\n" \
19 -o "$tmp" "$@" "$url" > "$stats"; then
20 rm -f "$tmp" "$stats"
21 brt_error "POST request failed"
22 exit 1
23 fi
24
25 cat "$tmp"
26 echo
27 brt_info "Response stats:"
28 cat "$stats"
29 rm -f "$tmp" "$stats"
30}
31