很抱歉问了这个愚蠢的问题,但我看到了 7 年前的这个帖子,但我想知道如何做同样的事情:
我看到“-i”标志传递给curl以获取“下一个”URL,但我仍然不完全确定如何循环标头中提供的后续URL,直到没有进一步的结果返回。感谢您的任何建议!
分页代码基于https://michaelheap.com/follow-github-link-header-bash/
# Set these:
url="https://COMPANY.okta.com/api/v1/users"
token="..."
# Pagination code based on https://michaelheap.com/follow-github-link-header-bash/
while [ "$url" ]; do
r=$(curl --compressed -Ss -i -H "authorization: SSWS $token" "$url" | tr -d '\r')
echo "$r" | sed '1,/^$/d' | jq -r '.[].profile.login'
url=$(echo "$r" | sed -n -E 's/link: <(.*)>; rel="next"/\1/pi')
done
此版本的 shell 脚本具有更好的变量名称,使其更易于理解。
正如您在问题中发布的那样,使用
curl
或 -i
选项运行 --include
会包含响应标头。
我在 How to get next page from Okta api call with CURL 上发布了相同的更新,以及 Python 版本以及为什么您可能想要使用 Python 而不是curl 的解释。
#!/usr/bin/env bash
# Set these:
url='https://COMPANY.okta.com/api/v1/users'
token='...'
# Pagination code based on https://michaelheap.com/follow-github-link-header-bash
while [ "$url" ]; do
r=$(curl --compressed -isSH "authorization: SSWS $token" "$url" | tr -d '\r')
headers=$(echo "$r" | sed '/^$/q')
body=$(echo "$r" | sed '1,/^$/d')
echo "$body" | jq -r '.[].profile.login'
url=$(echo "$headers" | sed -n -E 's/link: <(.*)>; rel="next"/\1/pi')
done