【问题标题】:Shell script call API via curl and process response通过 curl 和进程响应的 Shell 脚本调用 API
【发布时间】: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}"

我在给定脚本中的问题是:

  1. 没有得到 curl 调用 API 的响应。
  2. 从响应 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 值?

谢谢!

【问题讨论】:

    标签: shell curl jwt


    【解决方案1】:

    您的 curl 语句中有错误。您正在使用目标 URL 作为标头字段来执行它:

    curl --request POST -H "Content-Type:application/json" -H http://acme.com/api/authentications/login --data "${PAYLOAD}"
                                                           ^
                                                           |
                                                       Remove this header flag
    

    当从脚本执行 curl 时,静默 -s 标志也有帮助:

    -s, --silent 静音或静音模式。不要显示进度表或错误消息。使 Curl 静音。

    之后,您可以将数据存储在变量中并对其执行正则表达式以提取您需要进行进一步处理的令牌。

    完整的脚本可能如下所示:

    #!/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"}'
    
    RESPONSE=`curl -s --request POST -H "Content-Type:application/json" http://acme.com/api/authentications/login --data "${PAYLOAD}"`
    
    TOKEN=`echo $RESPONSE | grep -Po '"token":(\W+)?"\K[a-zA-Z0-9._]+(?=")'`
    
    echo "$TOKEN" # Use for further processsing
    

    【讨论】:

    • 我的正则表达式有问题,请参阅regexr.com/4eccp 我不能只提取token 的值。
    • 尝试以下正则表达式:"token":(\W+)?"\K[a-zA-Z0-9._]+(?=")。您提供的在线工具使用 JavaScript 正则表达式语法,grep 使用 Perl 正则表达式。试试regex101.com 另外:在此处发布 JWT 令牌时要小心。粘贴时包含机密信息,例如在jwt.io
    • 如果我硬编码 RESPONSE=''... 正则表达式有效。但如果我使用 RESPONSE='' 它不会返回任何内容(令牌为空白)。
    • 真的很接近,如果我对响应进行硬编码,则正则表达式可以工作..但如果我调用 curl...响应看起来正确...但正则表达式不起作用..它无法返回令牌(空/空白)
    • @PakiPat 缺少单引号。我编辑了我的原始帖子。谢谢!
    【解决方案2】:

    使用正则表达式解析 JSON 的另一种解决方案是 jq

    echo '{ "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" }' | jq -r '.user.token'
    

    【讨论】:

    • 恐怕在我的情况下安装其他命令不是一个选项(jq)
    猜你喜欢
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 2018-07-29
    • 1970-01-01
    • 2013-11-11
    • 1970-01-01
    相关资源
    最近更新 更多