#!/usr/bin/env bash

brt_post() {
  local url="${1:-}"
  if [[ -z "$url" ]]; then
    brt_error "Usage: brt -post <url> [curl flags...]"
    brt_info "Example: brt -post https://httpbin.org/post -H 'Content-Type: application/json' -d '{\"ok\":true}'"
    exit 1
  fi
  shift

  brt_require curl

  local tmp stats
  tmp=$(mktemp)
  stats=$(mktemp)

  if ! curl -sS -X POST -w "http_code:%{http_code}\nsize:%{size_download}\ntime:%{time_total}\n" \
    -o "$tmp" "$@" "$url" > "$stats"; then
    rm -f "$tmp" "$stats"
    brt_error "POST request failed"
    exit 1
  fi

  cat "$tmp"
  echo
  brt_info "Response stats:"
  cat "$stats"
  rm -f "$tmp" "$stats"
}
