akka-http / routing DSL: How to set response content type?

2,707 views
Skip to first unread message

Jo Kade

unread,
Dec 10, 2014, 9:20:33 AM12/10/14
to akka...@googlegroups.com
What is the intended way to set the response content type from within a route?
In spray there is a respondWithMediaType() directive, but akka-http has not (yet?).
I cannot use respondeWithHeader(`Content-Type`()) since it has a package-private constructor,
and using RawHeader("Content-Type","text/html") results in an error. Thanks,

Best regards,

Johannes

Björn Antonsson

unread,
Dec 11, 2014, 5:45:45 AM12/11/14
to akka...@googlegroups.com
Hi Johannes,

It seems like there is a mismatch here. The only way that I can find is to use the Java API like this

respondWithHeaders(akka.http.model.japi.headers.ContentType.create(ContentType(`text/html`)))


B/
--
>>>>>>>>>> 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 akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

-- 
Björn Antonsson
Typesafe – Reactive Apps on the JVM
twitter: @bantonsson

Björn Antonsson

unread,
Dec 11, 2014, 7:57:01 AM12/11/14
to akka...@googlegroups.com
Hi again Johannes,

Mathias was kind enough to educate me in the ticket that the respondWithMediaType way of doing things has been removed since it was considered an antipattern and you should now use the Marshalling infrastrucutre instead to acheive the same results.

Please take a look at akka.http.marshalling.Marshaller. You can use the PredefinedToEntityMarshallers and change the content type with the wrap method on the marshaller.

B/

Jo Kade

unread,
Dec 11, 2014, 8:01:49 AM12/11/14
to akka...@googlegroups.com
Hi Björn,

thanks for the reply; however, although this solution compiles, I get a runtime warning:

Explicitly set HTTP header 'Content-Type: text/html' is ignored, explicit `Content-Type` header is not allowed.
Set `HttpResponse.entity.contentType` instead.

-- Johannes

André

unread,
Dec 12, 2014, 5:22:06 AM12/12/14
to akka...@googlegroups.com
Hi Jo,

as Björn and Mathias exlained there's no need to set the Content-Type explicitly. Just import ScalaXmlSupport and complete with a NodeSeq (example) or alternatively implicitly define a string marshaller for text/html and complete with a String.

What exactly did you try to trigger this warning?
Reply all
Reply to author
Forward
0 new messages