Question

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 ?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top