0

I'm fighting against REST API that performs a 304 redirect; what I need is to take the destination URL of the redirect and open it with the browser (I know, it's a little of a perversion). I successfully intercepted the redirect, thanks to this nice lad reversepanda: https://github.com/bustoutsolutions/siesta/issues/210 but still I didn't figure out how to take the redirect url in the callback of the GET request (success or failure)

resource.load().onSuccess{ response in
        //HERE I WOULD LIKE TO TAKE THE URL OF THE REDIRECT 
        //(if I print the response I can see the HTML of the destination web page where the user should land)
    }.onFailure{ error in
        //using 'completionHandler(nil)' in the 'willPerformHTTPRedirection' method of the delegate, brings me here
    }

Any suggestion on how I could resolve this problem?

Thank you!

AleGiovane
  • 172
  • 1
  • 13
  • (1) 304 redirect?? Do you mean 301 or 302?(2) Does the Location header not appear in the response entity? – Paul Cantrell Jun 12 '18 at 15:37
  • Sorry, I meant 307 (it was a typo). How can I retrieve the location from the header? If I print all the response.headers, I don't see a "location" – AleGiovane Jun 13 '18 at 19:56
  • Siesta’s `Entity` type has a [`headers`](https://bustoutsolutions.github.io/siesta/api/Structs/Entity.html#/s:6Siesta6EntityV7headerss10DictionaryVyS2SGv) property. – Paul Cantrell Jun 13 '18 at 21:31
  • correct, that's what I'm talking about when I say that I'm printing "response.headers". But in the headers I can't see the location – AleGiovane Jun 14 '18 at 06:11

1 Answers1

0

Take a look inside RequestChain.swift, it has some comments with example which can help. I believe you can do something like:

func redirectRequest() -> Request {
  return self.yourAnotherRequest(onSuccess: {
    }, onFailure: { error in
  })
}

func yourRequest(request: Siesta.Request) -> Request {
  return request.chained {
    guard case .failure(let error) = $0.response,
        error.httpStatusCode == 401 else {
            return .useThisResponse
    }

    return .passTo(
        self.redirectRequest().chained {
            if case .failure = $0.response {
                return .useThisResponse
            } else {
                return .passTo(request.repeated())
            }
        }
    )
  }
}

You can search more examples with keywords chained, useThisResponse and passTo in Siesta sources.

Please, let us know if it helps to solve your issue and it would be nice to see your final solution.

vaa
  • 164
  • 15