Fernando F. Freire
Fernando F. Freire

Reputation: 47

Follow links in cURL responses using a loop

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

the response I get is

{"next":"/gibberish"}

So I perform another request:

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

the response I get is

{"next":"/anotherGibberish"}

I want to be able to do the curl request, get the next information and loop in to the next request.

Do you guys have any idea how to perform this in bash?

EDIT: Any help using jq would also be great. The last response is a JSON response with an authentication failure - that's where I should stop the loop.

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

Upvotes: 1

Views: 294

Answers (1)

oguz ismail
oguz ismail

Reputation: 50775

Assuming the next key is either nonexistent in the response or its value is a string forming a valid URL when prepended to http://example.com:

#!/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

Upvotes: 1

Related Questions