很抱歉,我不能做到这一点:我需要添加一些json到一个帖子,所以遵循文档: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" }"""))而且它永远不会编译:
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"}"""))发布于 2013-12-12 05:40:27
也有同样的问题,并在这里找到了解决方案:
这对我来说终于起作用了:
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))
}发布于 2013-09-05 06:00:33
对不起,你的问题有点麻烦,至少对我来说是这样。如果你想在Spray中使用一些Json作为HttpEntity来发出POST请求,那么你应该尝试使用Spray Client流水线来实现,这非常简单。
您需要创建一个简单的管道:
val pipe: HttpRequest => Future[HttpResponse] = sendReceive然后构建一个请求:
import spray.json.SprayJsonSupport._
pipe(Post("/api/1.0/users/ping", """{ "key"="whatever" }""".asJson))这将返回一个带有HttpResponse的Future,如果你想要一些特定的结果,比如说,一些确认码,那么在你的管道中添加unmarshall步骤:
val pipe: HttpRequest => Future[ConfCode] = sendReceive ~> unmarshal[ConfCode]发布于 2013-09-05 20:59:07
这也是我尝试过的,但它不起作用它告诉我我缺少一个隐含的:
val pipeline = sendReceive(conduit)
val responseF = pipeline(Post("/api/1.0/users/ping.json", """{ "key": "whatever" }""".asJson))
responseF onComplete { ...但我总是得到:
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))此外,您在上一个快照中的导入也不起作用...我使用的是不好的库版本吗?我的sbt设置为:
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,https://stackoverflow.com/questions/18623356
复制相似问题