【发布时间】:2019-05-21 06:54:13
【问题描述】:
我需要创建一个通过 curl 调用我的登录 API 的 shell 脚本。 该脚本应该能够存储和处理来自 curl api 调用的响应。
myscript.sh
#!/bin/bash
echo "Extract bearer token from curl calling login api"
echo
# Check cURL command if available (required), abort if does not exists
type curl >/dev/null 2>&1 || { echo >&2 "Required curl but it's not installed. Aborting."; exit 1; }
echo
PAYLOAD='{"email": "dummy-user@acme.com", "password": "secret"}'
curl -s --request POST -H "Content-Type:application/json" http://acme.com/api/authentications/login --data "${PAYLOAD}"
我在给定脚本中的问题是:
- 没有得到 curl 调用 API 的响应。
- 从响应 json 中,仅获取令牌值。
登录 API 响应示例:
{
"user": {
"id": 123,
"token": "<GENERATED-TOKEN-HERE>",
"email": "dummy-user@acme.com",
"refreshToken": "<GENERATED-REFRESH-TOKEN>",
"uuid": "1239c226-8dd7-4edf-b948-df2f75508888"
},
"clientId": "abc12345",
"clientSecretKey": "thisisasecret"
}
我只需要获取token 的值并将其存储在一个变量中...我将在其他 curl api 调用中使用令牌值作为承载令牌。
我需要在脚本中进行哪些更改才能从 curl api 调用的响应中提取 token 值?
谢谢!
【问题讨论】: