命令行版 postman 之 post 小工具

发布于:2024-11-28 ⋅ 阅读:(11) ⋅ 点赞:(0)

依赖

  1. curl
  2. jq

post.sh

#!/bin/bash

BASE='http://119.119.119.119'
METHOD=$1
URL=$BASE/$2
LOGIN=$BASE/login
echo $URL
token=$(curl --silent $LOGIN   -H 'Accept: application/json, text/plain, */*'   -H 'Accept-Language: zh-CN,zh;q=0.9'   -H 'Connection: keep-alive'   -H 'Content-Type: application/json;charset=UTF-8'   -H 'DNT: 1'   -H 'Origin: http://10.10.200.250'   -H 'Referer: http://10.10.200.250/'   -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'   --data '{"username":"admin","password":"admin123"}'   --insecure | jq .token | tr -d '"')
echo $token



send_get(){
  curl --silent $URL \
    -H 'Accept: application/json, text/plain, */*' \
    -H 'Accept-Language: zh-CN,zh;q=0.9' \
    -H 'Authorization: Bearer '$token \
    -H 'Connection: keep-alive' \
    -H 'Content-Type: application/json;charset=UTF-8' \
    -H 'Cookie: ttpt-Admin-Token='$token \
    -H 'DNT: 1' \
    -H 'Origin: '$BASE \
    -H 'Referer: '$BASE \
    -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36' \
    --insecure | jq '.' | tee response.json
}

send_post(){
  curl --silent $URL \
    -H 'Accept: application/json, text/plain, */*' \
    -H 'Accept-Language: zh-CN,zh;q=0.9' \
    -H "Authorization: Bearer $token" \
    -H 'Connection: keep-alive' \
    -H 'Content-Type: application/json;charset=UTF-8' \
    -H "Cookie: ttpt-Admin-Token=$token" \
    -H 'DNT: 1' \
    -H "Origin: $BASE" \
    -H "Referer: $BASE" \
    -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36' \
    --data @data.json \
    --insecure | jq '.' | tee response.json
}

case $METHOD in
  get)
   send_get
  ;;
  post)
   send_post
  ;;
  *)
    echo "http  $METHOD not supported"
  ;;
esac

post 请求参数写到 data.json 里