check_writer
check_writer

Reputation: 61

scala-spray: why do I get escaped strings with trailing quotes?

I'm new to scala and spray and I'm having a very simple problem. I want my rest service to return unescaped strings when I return a string from a rest call, it stead i'm getting a compacted string with escapes in it. Here is my code:

Rest Service: ....

get {
     respondWithMediaType(MediaTypes.`text/plain`) {
        complete {
                    s"""
hey joe this is 
"me" 
bill"""
          }
      }
   }

When I call the my service I get:

"\nhey joe this is\n\"me\"\n\bill"

But if I do a println() I see what I would expect. Please help.

Upvotes: 1

Views: 1379

Answers (2)

kadr68
kadr68

Reputation: 51

We had the same problem using Jackson; we worked around the issue by creating an http response object instead, like so…

              respondWithMediaType(`text/html`) {
                complete {
                  new HttpResponse(StatusCodes.OK, HttpEntity(
                    """
                    |<!DOCTYPE html>
                    |<html>
                    |<head>
                    |<title>Download app</title>
                    |</head>
                    |<body>
                    |<h2>
                    |Click <a href="/download/test.txt">here</a><br>
                    |</h2>
                    |</body>
                    |</html>
                  """.stripMargin
                  ))
                }
              }

Upvotes: 3

check_writer
check_writer

Reputation: 61

I solved my own problem, it was the JSON support trait that was automatically marshalling the object

This trait Json4sSupport automatally marshal your objects as JSON hence the wrapping and compact print etc.

import spray.httpx.Json4sSupport

If you remove it you should be getting the unescaped results.

hope it helps anyone else out there.

Upvotes: 4

Related Questions