scalahttp-headersdatabinderscala-dispatch

Getting a 302 http redirect location with Databinder-Dispatch


With databinder-dispatch 0.8.3 I'm trying to get the redirect url from a 302 http response, but I get the following exception:

Caused by: dispatch.StatusCode: Unexpected response code: 302

Here's what I've attempted, with some help:

import dispatch._
import org.apache.http.{HttpRequest,HttpResponse}
import org.apache.http.protocol.HttpContext
import org.apache.http.HttpStatus._

val http = new Http { 

  override def make_client = { 
    val client = new ConfiguredHttpClient(new Http.CurrentCredentials(None))

    client.setRedirectStrategy(new org.apache.http.impl.client.DefaultRedirectStrategy {      
        override def isRedirected(req: HttpRequest, res: HttpResponse, ctx: HttpContext) = false 
    })

    client 
  }
}

val req: Request = :/("graph.facebook.com") / "kmels" / "picture" 

val pictureURL: String = http(req.secure >:> { _("Location").head }) //error prone code, but we're just testing. This is the line where the exception is thrown.

What am I missing? as always, thanks in advance.


Solution

  • The following works:

    val pictureURL: String = http.x(req.secure >:> { _("Location").head })