Avoid processing of xml output.

14 views
Skip to first unread message

Vicki Le

unread,
Apr 29, 2015, 2:39:40 AM4/29/15
to spray...@googlegroups.com
Hi!

I'm running into a problem where I want to respond with xml content using the following code:

val body = HttpEntity(
    contentType
= ContentType(`application/xml`, `UTF-8`),
   
string = responseAsString)
val response
= HttpResponse(
    status
= StatusCodes.OK,
    entity
= body)

This works but my responseAsString has a CDATA section in it with illegal XML characters within it. It looks like spray is ignoring the CDATA and escaping all the illegal XML characters. Is there a way to avoid this while keeping the media type as xml? I found that when i use text/plain, the response comes out the way I want it to.

Thanks!

Johannes Rudolph

unread,
Apr 29, 2015, 2:55:39 AM4/29/15
to spray...@googlegroups.com
Hi Vicki,

On Tue, Apr 28, 2015 at 10:19 PM, Vicki Le <vi...@vicnle.com> wrote:
> This works but my responseAsString has a CDATA section in it with illegal
> XML characters within it. It looks like spray is ignoring the CDATA and
> escaping all the illegal XML characters. Is there a way to avoid this while
> keeping the media type as xml? I found that when i use text/plain, the
> response comes out the way I want it to.

If you create an HttpResponse manually like you do in the example, the
only thing that spray does in `HttpEntity.apply(ContentType, String)`
is applying the charset from the content type to encode the string to
create the binary data. Spray shouldn't touch the content in any way,
in particular it doesn't care if the content is XML or not (at least
in that code snippet).

So, I suspect the problem lies somewhere else. This could be either
the rest of your server code (do you use spray-routing?) or it could
also happen on the client which may interpret `application/xml` data
differently from `text/plain` data. Maybe you can show more code or a
data example which reproduces the problem?

Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

Vicki

unread,
Apr 29, 2015, 9:55:53 AM4/29/15
to spray...@googlegroups.com
Hi Johannes,

I went back to my code to check what could possibly be the problem and, given your advice, I found out it was the REST client I was using. It was trying to pretty print the xml and happened to just ignore CDATA sections.

Many thanks for the help!
Vicki
Reply all
Reply to author
Forward
0 new messages