Sending post with json using spray?

2019-04-12 09:10发布

Sorry I can't manage to make this work: I need to add some json to a post, so following the documentation: http://spray.io/documentation/1.1-M8/spray-httpx/request-building/ :

import scala.util.{Success, Failure}
import akka.actor.{Props, ActorSystem}
import spray.can.client.DefaultHttpClient
import spray.client.HttpConduit
import spray.httpx.SprayJsonSupport
import spray.http._
import spray.json.JsonParser._
import spray.json._
import HttpMethods._
import HttpHeaders._
import MediaTypes._
import spray.httpx.RequestBuilding._
import scala.concurrent.ExecutionContext.Implicits.global

...

val req = HttpRequest(method = POST, uri = "/api/1.0/users/ping.json", entity = HttpEntity(`application/json`,"""{ "key"="whatever" }"""))

and it never compiles:

overloaded method value apply with alternatives:
[error]   (optionalBody: Option[spray.http.HttpBody])spray.http.HttpEntity <and>
[error]   (buffer: Array[Byte])spray.http.HttpEntity <and>
[error]   (string: String)spray.http.HttpEntity
[error]  cannot be applied to (spray.http.MediaType, String)
[error]     val req = HttpRequest(method = POST, uri = "/api/1.0/users/ping.json", entity = HttpEntity(`application/json`,"""{ "key"="whatever"}"""))

标签: scala spray
3条回答
▲ chillily
2楼-- · 2019-04-12 09:35

Sorry, but you question is a bit cumbersome, for me at least. If you want to make a POST request in Spray with some Json as HttpEntity, then you should try to do it with Spray-Clients pipelining, it's drop dead simple.

You need to create a simple pipe:

val pipe: HttpRequest => Future[HttpResponse] = sendReceive

and then build a request:

import spray.json.SprayJsonSupport._
pipe(Post("/api/1.0/users/ping", """{ "key"="whatever" }""".asJson))

This will return you a Future with HttpResponse, if you want some specific result, let's say, for example, some confirmation code, then add unmarshall step to your pipe:

val pipe: HttpRequest => Future[ConfCode] = sendReceive ~> unmarshal[ConfCode]
查看更多
\"骚年 ilove
3楼-- · 2019-04-12 09:39

Had the same problem and found the solution here:

https://github.com/spray/spray-json/blob/master/src/main/scala/spray/json/AdditionalFormats.scala#L30-41

This finally worked for me:

import spray.httpx.SprayJsonSupport
import spray.json.AdditionalFormats

object Client extends SprayJsonSupport with AdditionalFormats {
    val email = "..."
    val password = "..."
    val pipeline = sendReceive       
    pipeline(Post("http://something.com/login", s"""{
        "email": "$email",
        "password": "$password"
    }""".asJson.asJsObject))
}
查看更多
Evening l夕情丶
4楼-- · 2019-04-12 09:41

That is what i tried too, but it doesn't work it is telling me that I am missing an implicit:

val pipeline = sendReceive(conduit)
val responseF = pipeline(Post("/api/1.0/users/ping.json", """{ "key": "whatever" }""".asJson))

responseF onComplete { ...

but i always get :

could not find implicit value for evidence parameter of type spray.httpx.marshalling.Marshaller[spray.json.JsValue]
[error]     val responseF = pipeline(Post("/api/1.0/users/ping.json", """{ "key": "whatever" }""".asJson))

also the import you have in your previous snapshot does not work.... Am i using a bad version of the library? My sbt settings are:

val sprayVersion = "1.1-M7"
val akkaVersion = "2.1.1"
"io.spray" %% "spray-json"   % "1.2.5",
    "com.typesafe.akka" %% "akka-actor" % akkaVersion,
    "com.typesafe.akka" %% "akka-slf4j" % akkaVersion,
    "io.spray" %  "spray-client" % sprayVersion,
查看更多
登录 后发表回答