使用循环关注cURL响应中的链接


curl -H "Accept: application/json" 'http://example.com'

我得到的回复是

{"next":"/gibberish"}

所以我执行另一个请求:

curl -H "Accept: application/json" 'http://example.com/gibberish'

我得到的回复是

{"next":"/anotherGibberish"}

我希望能够执行curl请求,获得next信息并循环到下一个请求。

你们知道如何在bash中表演吗?

编辑:任何使用jq的帮助都会很棒。最后一个响应是一个身份验证失败的JSON响应——这就是我应该停止循环的地方。

{"unauthorized":"Authenticate with username and password"}

假设next密钥在响应中不存在,或者其值是一个字符串,在http://example.com:之前形成一个有效的URL

#!/bin/bash -
next=/
while resp=$(curl -sS "http://example.com$next"); do
if ! next=$(jq -er '.next' <<< "$resp"); then
# `resp' is the final response here
# , do whatever you want with it
break
fi
done

最新更新