diff options
author | adamw <adam@warski.org> | 2017-08-31 15:11:01 +0200 |
---|---|---|
committer | adamw <adam@warski.org> | 2017-08-31 15:11:01 +0200 |
commit | 666755ede0d221c11c1eac6aac57ce3d18c42c8b (patch) | |
tree | 497fb484a20b9f3aa6c424e9b2d0fdccf27591ab /core | |
parent | 9ec0994878856743e54c4d1a38c0c80b318d7be7 (diff) | |
download | sttp-666755ede0d221c11c1eac6aac57ce3d18c42c8b.tar.gz sttp-666755ede0d221c11c1eac6aac57ce3d18c42c8b.tar.bz2 sttp-666755ede0d221c11c1eac6aac57ce3d18c42c8b.zip |
Keep a history of redirect responses
Diffstat (limited to 'core')
3 files changed, 13 insertions, 3 deletions
diff --git a/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionHandler.scala b/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionHandler.scala index ddc1293..24c93f4 100644 --- a/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionHandler.scala +++ b/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionHandler.scala @@ -199,7 +199,7 @@ object HttpURLConnectionHandler extends SttpHandler[Id, Nothing] { Left(readResponseBody(wrappedIs, asString)) } - Response(body, code, headers) + Response(body, code, headers, Nil) } private def readResponseBody[T](is: InputStream, diff --git a/core/src/main/scala/com/softwaremill/sttp/Response.scala b/core/src/main/scala/com/softwaremill/sttp/Response.scala index 5a3cfe3..7551ee0 100644 --- a/core/src/main/scala/com/softwaremill/sttp/Response.scala +++ b/core/src/main/scala/com/softwaremill/sttp/Response.scala @@ -21,10 +21,14 @@ import scala.util.Try * `Left(String)`, if the request wasn't successful (status code * 3xx, 4xx or 5xx). In this case, the response body is read into * a `String`. + * @param history If redirects are followed, and there were redirects, + * contains responses for the intermediate requests. + * The first response (oldest) comes first. */ case class Response[T](body: Either[String, T], code: Int, - headers: Seq[(String, String)]) { + headers: Seq[(String, String)], + history: List[Response[Unit]]) { def is200: Boolean = code == 200 def isSuccess: Boolean = codeIsSuccess(code) def isRedirect: Boolean = code >= 300 && code < 400 diff --git a/core/src/main/scala/com/softwaremill/sttp/SttpHandler.scala b/core/src/main/scala/com/softwaremill/sttp/SttpHandler.scala index 26464ba..2f44840 100644 --- a/core/src/main/scala/com/softwaremill/sttp/SttpHandler.scala +++ b/core/src/main/scala/com/softwaremill/sttp/SttpHandler.scala @@ -38,7 +38,13 @@ trait SttpHandler[R[_], -S] { uri"$loc" } - send(request.copy[Id, T, S](uri = uri)) + val redirectResponse = send(request.copy[Id, T, S](uri = uri)) + + responseMonad.map(redirectResponse) { rr => + val responseNoBody = + response.copy(body = response.body.right.map(_ => ())) + rr.copy(history = responseNoBody :: rr.history) + } } } |