David McHealy
David McHealy

Reputation: 2481

Getting headers from dispatch library (version 0.10.1)

This is technically a duplicate to this thread:

Return exact response/header?

However, the code in there doesn't seem to work for me. I see in the changelogs for dispatch that handing has changed subtley, but for the life of me I cannot get this to work. I think this is a problem with me understanding scala and not a library problem.

import dispatch._, Defaults._

// import com.ning.http.client.Response

object HTTPDownloader extends App {


  val goog: Req = host("google.com").secure

  val res = Http(goog.HEAD OK as.Response).option()

  print(res)
  Thread.sleep(5000)
}

I'm trying to get headers from a website that I know is up so that I can check the content length, but the error I get when I compile this snippet is

[error] ... Download.scala:14: type mismatch;
[error]  found   : dispatch.as.Response.type
[error]  required: com.ning.http.client.Response => ?
[error]   val res = Http(goog.HEAD OK as.Response).option()

I try to import client.Response instead with import com.ning.http.client.{ Response => nonconflictingname }, but then it says "object com.ning.http.client.Response is not a value." I have no idea what that means.

Upvotes: 0

Views: 1203

Answers (2)

cmbaxter
cmbaxter

Reputation: 35463

If you wanted to get the entire response object and you don't care about non-OK status codes returning a failed future, then you could do something like this:

val res = Http(goog.HEAD)
res onComplete{
  case Success(resp) =>
    val headers = resp.getHeaders
    ...
  case Failure(ex) => //handle fail
}

If you wated to OK status handling, then it would look like this:

val res = Http(goog.HEAD OK(r => r))

Upvotes: 1

David McHealy
David McHealy

Reputation: 2481

I just looked at the types in the source of dispatch and realized the problem. I missed the little => ? at the required part of the error.

The right answer is something like

val res = Http(goog > as.Response( x => x.getHeaders)).option()

which makes perfect sense if you think about it, because it has no idea what to return unless you tell it.

Upvotes: 1

Related Questions