Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spray routing: How to respond with different content-types?

In spray I would like to respond with different content-types, depending on the given Accept header. I've seen a couple of suggestions in the question by rompetroll, but I would like to hear if there are any canonical way of doing it (i. e. simple or already implemented).

In essence what I imagine should happen is something like:

path("somepath") {
  get {
    // Find whatever we would like to return (lazily)
    ...
    // Marshall resource and complete depending on the `Accept` header
    ...
  }
}

Thanks in advance.

like image 779
Jens Egholm Avatar asked Jan 14 '14 16:01

Jens Egholm


2 Answers

See the tests in this commit.

I copied it here for reference:

case class Data(name: String, age: Int)
object Data {
  import spray.json.DefaultJsonProtocol._
  import spray.httpx.SprayJsonSupport._

  // don't make those `implicit` or you will "ambiguous implicit" errors when compiling
  val jsonMarshaller: Marshaller[Data] = jsonFormat2(Data.apply)
  val xmlMarshaller: Marshaller[Data] =
    Marshaller.delegate[Data, xml.NodeSeq](MediaTypes.`text/xml`) { (data: Data) ⇒
      <data><name>{ data.name }</name><age>{ data.age }</age></data>
    }

  implicit val dataMarshaller: ToResponseMarshaller[Data] =
    ToResponseMarshaller.oneOf(MediaTypes.`application/json`, MediaTypes.`text/xml`)  (jsonMarshaller, xmlMarshaller)
}

You then using complete should suffice in your route, content-type negotiation is automatically taken care of:

get {
  complete(Data("Ida", 83))
}
like image 145
jrudolph Avatar answered Nov 09 '22 02:11

jrudolph


Spray is actually looking into the Accept header value and validates against it. So if route is returning application/json or text/plain and client accepts image/jpeg than spray will return 406 Not Acceptable. If client will request application/json ortext/plain from this route than he will receive repsonse with matching Content-Type.

The main trick here is to use correct marshallers for return objects. You can read more about marshalling here.

Also you can override MediaType with respondWithMediaType directive, but I think it is better to use correct marshallers.

like image 45
vitalii Avatar answered Nov 09 '22 03:11

vitalii