我正在尝试使用http4s库。我试图用一些json负载向REST web服务发出一个POST请求。
当我阅读文档http://http4s.org/docs/0.15/时,我只能看到一个GET方法示例。
有人知道怎么写帖子吗?
发布于 2016-07-28 18:57:43
看起来,示例中提到的get
/getAs
方法只是fetch
方法的方便包装器。请参阅https://github.com/http4s/http4s/blob/a4b52b042338ab35d89d260e0bcb39ccec1f1947/client/src/main/scala/org/http4s/client/Client.scala#L116
使用Request
构造函数并将Method.POST
作为method
传递。
fetch(Request(Method.POST, uri))
发布于 2016-11-25 18:47:32
https4s版本: 0.14.11
最难的部分是如何设置柱身。当您深入研究代码时,您可能会发现type EntityBody = Process[Task, ByteVector]
。但是是吗?但是,如果您还没有准备好深入研究scalaz,只需使用withBody
即可。
object Client extends App {
val client = PooledHttp1Client()
val httpize = Uri.uri("http://httpize.herokuapp.com")
def post() = {
val req = Request(method = Method.POST, uri = httpize / "post").withBody("hello")
val task = client.expect[String](req)
val x = task.unsafePerformSync
println(x)
}
post()
client.shutdownNow()
}
P.S.我关于http4s客户机的有用文章(只需跳过中文并阅读scala代码):http://sadhen.com/blog/2016/11/27/http4s-client-intro.html
发布于 2016-09-22 13:59:32
import org.http4s.circe._
import org.http4s.dsl._
import io.circe.generic.auto._
case class Name(name: String)
implicit val nameDecoder: EntityDecoder[Name] = jsonOf[Name]
def routes: PartialFunction[Request, Task[Response]] = {
case req @ POST -> Root / "hello" =>
req.decode[Name] { name =>
Ok(s"Hello, ${name.name}")
}
希望这能有所帮助。
https://stackoverflow.com/questions/38644392
复制相似问题