I am using dispatch 0.11.0, Scala 2.10 and trying to get this code to work
val svc = url("https://my-server/img/cat.jpg").as(username, password)
val respBody = Http(svc OK as.Response(_.getResponseBodyAsStream))
respBody onComplete {
case Success(stream) => {
val fos = new java.io.FileOutputStream("myfile.jpg")
Iterator
.continually (stream.read)
.takeWhile (-1 !=)
.foreach (fos.write)
fos.close()
}
case Failure(exception) => {
Logger.log.error(exception.toString)
}
}
When the server returns 302, dispatch doesn't handle authenticaton properly and Failure was executed. The odd thing is if I point the url to a JSON endpoint which returns a 401, the authentication works just fine. I don't know why the server is setup to return 2 different statuses for unauthorized access but I need to figure out how to deal with this. Any insights will be much appreciated.
Thanks, Bob
Try as_!() instead of as(), it uses preemptive auth.
source:
def as(user: String, password: String) =
subject.setRealm(new RealmBuilder()
.setPrincipal(user)
.setPassword(password)
.build())
def as_!(user: String, password: String) =
subject.setRealm(new RealmBuilder()
.setPrincipal(user)
.setPassword(password)
.setUsePreemptiveAuth(true)
.setScheme(AuthScheme.BASIC)
.build())