【shell】 curl 命令

发布时间 2023-03-28 20:20:39作者: xiaoyu_jane
  1. Get
#直接拿值
nodes_account_info=`curl --header "Content-Type:application/json" -k -sS \
--header "X-Auth-Token:$MATRIX_TOKEN" --request GET \
--url "https://127.0.0.1:$MATRIX_PORT/matrix/rsapi/v1.0/cluster/nodes_account_info"`

2.Post

#直接拿值
token=$(curl -X POST -s -k -g --header 'Content-Type: application/json' \
--header 'Accept: application/json' \
-d '{"userName": "admin"}' "http://${tokenIp}:8088/token/auth/generate" |jq '.token' | sed 's/\"//g')
echo $token

#使用jq解析返回值
cfgresult=$(curl -g -s -X GET --header "Accept: application/json" --header "username: $cfgcentUser" --header "password: $cfgcentPwd" "$cfgurl")
echo $(echo $cfgresult | jq -r .value)

#只需要拿到返回码
postCode=$(curl -g -m 10 -o /dev/null -s -w %{http_code} -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header "username: $cfgcentUser" --header "password: $cfgcentPwd" -d "{\"key\":\"$cfgkey\",\"value\":\"$cfgvalue\"}" "$posturl")
if [[ $postCode -ne 200 && $postCode -ne 201 ]]; then
     write_log  ERROR "$posturl return $postCode ,not 200/201  code"
      return 1
fi
  1. put
#只需要拿到返回码
putCode=$(curl -g -m 10 -o /dev/null -s -w %{http_code} -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' --header "username: $cfgcentUser" --header "password: $cfgcentPwd" -d "{\"key\":\"$cfgkey\",\"value\":\"$cfgvalue\"}" "$puturl")
if [[ $putCode -ne 200 ]]; then
     write_log  ERROR "$puturl return $putCode ,not 200 code"
      return 1
fi