3

I'm using spray-client to access a REST service. Part of the data returned by the server is in the http response headers (the rest is in the response body).

In order to be able to unmarshall the response, I am using an Unmarshaller. However, the unmarshaller can only access the response body (as an instance of HttpEntity) and all headers seems to be unaccessible at this stage.

Here is the current pipeline and the unmarshaller codes:

  implicit val IDsUnmarshaller = 
    Unmarshaller[List[ID]](MediaTypes.`text/plain`) {
      case HttpEntity.NonEmpty(contentType, data) => 
        data.asString.split("\n").toList.map( ID(_) )
    }

  val pipeline: HttpRequest => Future[List[ID]] = (
    encode(Gzip)
    ~> sendReceive
    ~> decode(Deflate)
    ~> unmarshal[List[ID]]
  )

Is there anyway to access them when unmarshalling ? Is there any work around ?

paradigmatic
  • 40,153
  • 18
  • 88
  • 147
  • 1
    If you provide a `FromResponseUnmarshaller` instead of a plain `Unmarshaller` you also have access to the headers. – jrudolph Feb 25 '14 at 13:54
  • Thanks for the suggestion. Do you know if there is any factory method for creating `FromResponseUnmarshaller` ? – paradigmatic Feb 25 '14 at 14:36
  • 1
    See this file for ways to create `FromResponseUnmarshallers`: https://github.com/spray/spray/blob/master/spray-httpx/src/main/scala/spray/httpx/unmarshalling/Deserializer.scala – jrudolph Feb 25 '14 at 15:45
  • 2
    E.g. you can provide an implicit function `HttpResponse => List[ID]` and that should be picked up. – jrudolph Feb 25 '14 at 15:46
  • Thanks @jrudolph, it works and that's exactly what I was looking for. If you copy it in an answer I will acept it. – paradigmatic Feb 26 '14 at 19:11

1 Answers1

1

If you provide a FromResponseUnmarshaller instead of a plain Unmarshaller you also have access to the headers.

See this file for ways to create FromResponseUnmarshallers: https://github.com/spray/spray/blob/master/spray-httpx/src/main/scala/spray/httpx/unmarshalling/Deserializer.scala

E.g. you can provide an implicit function HttpResponse => List[ID] and that should be picked up.

jrudolph
  • 8,307
  • 4
  • 32
  • 50