Akka http - complete with headers

880 views
Skip to first unread message

Giovanni Alberto Caporaletti

unread,
Mar 11, 2015, 8:10:24 PM3/11/15
to akka...@googlegroups.com
Hi,
Completing a route with a Tuple3[StatusCode, Seq[HttpHeader], T]  doesn't seem to work for me. My json4s marshaller serializes the whole tuple as an object. Tuple2[StatusCode, T] works instead.
Are there any plans to implement it?

What's the best way to add headers to a response anyway? In my case the header depends on a future (or, well, on a materialization of a stream). 


Thank you

Cheers
G

Giovanni Alberto Caporaletti

unread,
Mar 11, 2015, 8:18:28 PM3/11/15
to akka...@googlegroups.com
This is what I came up with (I have another question below):

def onSuccessHead(magnet: OnStreamSuccessHeadMagnet): Directive[magnet.Out] = {
magnet.directive
}
trait OnStreamSuccessHeadMagnet {
type Out
def directive: Directive[Out]
}

object OnStreamSuccessHeadMagnet {
import akka.http.server.directives.FutureDirectives._

implicit def apply[T](s: Source[T, _])(implicit mat: ActorFlowMaterializer, tupler: Tupler[T]) = {
new OnStreamSuccessHeadMagnet {
type Out = tupler.Out
val directive = onSuccess(s.runWith(Sink.head("SourceMarshallerHeadSink")): Future[T])
}
}

}


and the route:
onSuccessHead(userService.createUser(req)) { newUser =>
respondWithHeader(Location(s"/$v1/$user/${newUser.id}")) {
complete(Created -> newUser)
}
}


By the way, is there a way of extracting "constant" paths? e.g. my path is path("v1" / "user") but I don't want to have separate constants for v1 and user and something like path("some" / "path") { (some, path) => ...} doesn't work. In other words, how do you do reverse routing in spray/akka http?

Thanks
G

Mark Hatton

unread,
Mar 12, 2015, 6:00:46 AM3/12/15
to akka...@googlegroups.com
Completing a route with a Tuple3[StatusCode, Seq[HttpHeader], T]  doesn't seem to work for me.

Ensure your headers collection is an instance of immutable.Seq.  E.g.:

import collection.immutable._

complete( (OK, Seq(`Cache-Control`(`no-cache`)),
responseEntity) )

Works for me.

Mark

 

Giovanni Alberto Caporaletti

unread,
Mar 12, 2015, 6:27:20 AM3/12/15
to akka...@googlegroups.com
oh! That was it! thanks ;) 

I'll remember to check if they add it to the documentation once it's released. 


Cheers
G
Reply all
Reply to author
Forward
0 new messages