Friday, October 28, 2022

[SOLVED] Follow links in cURL responses using a loop

Issue

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"}

Solution

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


Answered By - oguz ismail
Answer Checked By - Candace Johnson (WPSolving Volunteer)