On Friday, June 12, 2015 at 12:30:16 AM UTC+2, [email protected] wrote:
>
> I think ContentType is modeled directly on HttpEntity. Try a combination 
> of the mapResponseEntity-directive and the .withContentType-method on 
> HttpEntity.
>

To expand on this: 

In spray/akka-http some headers are treated specially because they 
logically belong somewhere else in the model. The content type is one of 
them which tightly belongs to the entity (= body) of an HTTP message in our 
view. (Also see the docs at [1] about this topic.)

So, how do you work with content-types?

There are basically two levels:

1) you construct the response manually with types from the model
2) you use the routing layer with `complete` and marshalling

In the first case you need to supply the content-type with the data, 
usually using one of the `HttpEntity.apply` methods [2]:

HttpEntity(MediaTypes.`text/plain`, "test")

In the second case you can still just pass an entity to `complete`: 

complete(HttpEntity(MediaTypes.`text/plain`, "test"))

or you make use of the marshalling infrastructure which uses type-classes 
to convert a value of your domain model to a acceptable entity. So, using a 
marshaller you can also be sure that content-types and charsets are 
properly negotiated with the client.

E.g.

complete("test")

will use the predefined `StringMarshaller` [3] which will answer requests 
that accept a `text/plain` response (rejecting others) and apply a charset 
that the client understands.

HTH
Johannes


[1] 
http://doc.akka.io/docs/akka-stream-and-http-experimental/1.0-RC3/scala/http/common/http-model.html#HTTP_Headers
[2] 
http://doc.akka.io/api/akka-stream-and-http-experimental/1.0-RC3/#akka.http.scaladsl.model.HttpEntity$
[3] 
https://github.com/akka/akka/blob/release-2.3-dev/akka-http/src/main/scala/akka/http/scaladsl/marshalling/PredefinedToEntityMarshallers.scala#L50

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to