【问题标题】:How do I include POST request data with their respective url at the end of this script?如何在此脚本的末尾包含 POST 请求数据及其各自的 url?
【发布时间】:2022-01-25 17:29:19
【问题描述】:
test_url() {
  local status=$(curl -o /dev/null -s -w "%{http_code}\n" -H "Authorization: token abc123" -H "Content-Type: application/json" --request POST -d '{"abc":"123", "xyz":"456"}' "$1")

  if [ "$status" = 200 ]; then
    echo "$2 is running successfully"
  else
    echo "Error at $2. Status code: $status"
  fi
}

test_url https://url_1.com url_1
test_url https://url_2.com url_2

上面的脚本所做的只是打印 url_1 is running successfulError at url_2。状态码:401,取决于状态码。

假设 "abc":"123" 是 url_1 的 POST 请求数据,而 "xyz":"456" 是 url_2 的请求数据。同样,可能有很多数据,将它们全部放在一行中会令人困惑。所以我想将数据移动到它们各自的最后的url。比如:

test_url '{"abc":"123"}' https://url_1.com url_1
test_url '{"xyz":"456"}' https://url_2.com url_2

我知道这是一个错误的格式,只是想举个例子。那么正确的做法是什么呢?

【问题讨论】:

    标签: linux bash curl post script


    【解决方案1】:

    你可以试试这个:

    local status=$(curl -o /dev/null -s -w "%{http_code}\n" -H "Authorization: token abc123" -H "Content-Type: application/json" -d "$1" -X POST $2)
    

    打电话:

    test_url '{"abc":"123"}' "https://url_1.com"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-05
      • 1970-01-01
      相关资源
      最近更新 更多