spray

Get Json Object using spray.json

喜你入骨 提交于 2019-12-05 18:22:22
I'm using spray and I need to return a json object through a method. val route = path("all-modules") { get { respondWithMediaType(`text/html`) { complete( configViewer.findAllModules.toString) } } } This prints ConfigResults(S1000,Success,List(testDataTypes, mandate, sdp)) But I need get this as the json object. how can I do it? I tried in this way val route = path("all-modules") { get { respondWithMediaType(`application/json`) { complete{ configViewer.findAllModules } } } } It gives an compilation error could not find implicit value for parameter marshaller: spray.httpx.marshalling

How to match specific accept headers in a route?

守給你的承諾、 提交于 2019-12-05 17:58:12
I want to create a route that matches only if the client sends a specific Accept header. I use Spray 1.2-20130822 . I'd like to get the route working: def receive = runRoute { get { path("") { accept("application/json") { complete(...) } } } } Here I found a spec using an accept() function, but I can't figure out what to import in my Spray-Handler to make it work as directive. Also, I did not find other doc on header directives but these stubs . I would do this way: def acceptOnly(mr: MediaRange*): Directive0 = extract(_.request.headers).flatMap[HNil] { case headers if headers.contains(Accept

Spray-can NoClassDefFoundError

牧云@^-^@ 提交于 2019-12-05 15:33:50
I'm new to spray, i can't get it to work... :/ My build.sbt: val apacheDeps = Seq( "commons-validator" % "commons-validator" % "1.4.1" ) val sprayAndAkkaDeps = { val sprayV = "1.3.3" Seq( "io.spray" %% "spray-can" % sprayV, "io.spray" %% "spray-routing" % sprayV, "io.spray" %% "spray-testkit" % sprayV % "test", "com.typesafe.akka" %% "akka-actor" % "2.3.9" ) } name := "myApp" version := "1.0.0" scalaVersion := "2.11.6" libraryDependencies ++= Seq( "org.mongodb" %% "casbah" % "2.8.1", "ch.qos.logback" % "logback-classic" % "1.1.2", "org.scala-lang.modules" %% "scala-xml" % "1.0.3", "com

Query parameters for GET requests using Akka HTTP (formally known as Spray)

孤者浪人 提交于 2019-12-05 14:23:14
问题 One of the features of Akka HTTP (formally known as Spray) is its ability to automagically marshal and unmarshal data back and forth from json into case classes, etc. I've had success at getting this to work well. At the moment, I am trying to make an HTTP client that performs a GET request with query parameters. The code currently looks like this: val httpResponse: Future[HttpResponse] = Http().singleRequest(HttpRequest( uri = s"""http://${config.getString("http.serverHost")}:${config.getInt

Use Future in Spray Routing

懵懂的女人 提交于 2019-12-05 08:51:48
I'm new to asynchronous programming. I read this tutorial http://danielwestheide.com/blog/2013/01/09/the-neophytes-guide-to-scala-part-8-welcome-to-the-future.html and was surprised by how effortless I can incorporate Future into the program. However, when I was using Future with Routing, the return type is kind of wrong. get { optionalCookie("commToken") { case Some(commCookie) => val response = (MTurkerProgressActor ? Register).mapTo[..].map({...}) val result = Await.result(response, 5 seconds) setCookie(HttpCookie("commToken", content = result._2.mturker.get.commToken)) { complete(result._1

How can I simulate a POST request with a json body in SprayTest?

流过昼夜 提交于 2019-12-05 08:13:45
If I have an endpoint that unmarshalls json like this: (path("signup")& post) { entity(as[Credentials]) { credentials => … How can I test that with a Spray test spec: "The Authentication service" should { "create a new account if none exists" in { Post("/api/authentication/signup", """{"email":"foo", "password":"foo:" }""") ~> authenticationRoute ~> check { handled === true } } } That obviously doesn't work for several reasons. What would be the correct way? 4lex1v The trick is to set the correct Content-Type: Post("/api/authentication/signup", HttpBody(MediaTypes.`application/json`, """{

Parsing a simple array with Spray-json

吃可爱长大的小学妹 提交于 2019-12-05 05:29:53
I'm trying (and failing) to get my head around how spray-json converts json feeds into objects. If I have a simple key -> value json feed then it seems to work ok but the data I want to read comes in a list like this: [{ "name": "John", "age": "30" }, { "name": "Tom", "age": "25" }] And my code looks like this: package jsontest import spray.json._ import DefaultJsonProtocol._ object JsonFun { case class Person(name: String, age: String) case class FriendList(items: List[Person]) object FriendsProtocol extends DefaultJsonProtocol { implicit val personFormat = jsonFormat2(Person) implicit val

Akka-http: How to get custom header from a request?

梦想的初衷 提交于 2019-12-05 00:53:11
问题 I send following headers in a request to my akka-http api: "Content-type": "application/json" , "Accept": "application/json" , "AppId": "some_id" . How do I get "AppId" custom header in my akka-http route? (get & parameters("id")) { (id) => complete { val appId = ?? // I want to get custom header here. } } Thanks. 回答1: You need to use one of the HeaderDirectives (HeaderDirectives docs) to extract the header. For example, if it's a custom one you can use headerValueByName which yields the

akka-http: How to set response headers

陌路散爱 提交于 2019-12-04 20:11:49
问题 I've a route as follows: val route = { logRequestResult("user-service") { pathPrefix("user") { get { respondWithHeader(RawHeader("Content-Type", "application/json")) { parameters("firstName".?, "lastName".?).as(Name) { name => findUserByName(name) match { case Left(users) => complete(users) case Right(error) => complete(error) } } } } ~ (put & entity(as[User])) { user => complete(Created -> s"Hello ${user.firstName} ${user.lastName}") } ~ (post & entity(as[User])) { user => complete(s"Hello $

marathon-lb health check failing on all spray.io containers

馋奶兔 提交于 2019-12-04 19:17:23
I'm running DC/OS 1.7 with marathon-lb. spray.io 1.3.3 is returning 400 to all marathon-lb/HAProxy heath check calls: request has a relative URI and is missing a Host header so marathon-lb never routes any requests to the service. The health check in the marathon json is: "healthChecks": [ { "path": "/health", "protocol": "HTTP", "portIndex": 0, "gracePeriodSeconds": 10, "intervalSeconds": 2, "timeoutSeconds": 10, "maxConsecutiveFailures": 10, "ignoreHttp1xx": false } ], and the logging by spray.io in the docker container is: [WARN] [08/19/2016 23:53:42.534] [asp-service-akka.actor.default