如何使用scala调度库向Lift中的服务器发送post请求?
我想使用scala调度库向Lift中的服务器发送post请求。
我想向外部服务器发送一个发布请求并获取一些信息,然后在我的网络应用程序中使用这些信息。
我该怎么做?
I want to use the scala dispatch library to send a post request to the server in the Lift.
I want to send a post request to the external server and get some information and then use this information in my web app.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
只为一人2024-08-26 00:57:40
以下是向服务器分派 REST 调用的代码片段:
val http = new Http
val call = parse(event.call)
val verbspec = (call \ "verb").values toString
val urlspec = (call \ "url").values toString
val namespec = (call \ "username").values toString
val pwspec = (call \ "password").values toString
val req = url(urlspec).as(namespec, pwspec) <:< Map("Content" -> "application/json")
val (status: Int, contentWrap, headers) = verbspec match {
case "GET" => {
http x (( req >:> identity ) {
case (200, _, Some(thing), out) => {
val resp = fromInputStream(thing.getContent()).getLines.mkString
(200, Some(resp), out())
}
case (badCode, _, _, out) => (badCode, None, out())
})
}
case "POST" => {
http x (( req.POST << (event.payload) >:> identity ) {case (status, _, _, out) => (status, None, out()) })
}
case "PUT" => {
http x (( req.PUT <<< (event.payload) >:> identity ) {case (status, _, _, out) => (status, None, out()) })
}
case _ => {
EventHandler.error(this, "Bad verb specified")
(000, None, Map.empty)
}
}
其中:
event.call
-> json 指定调用
event.payload
-> PUT 和 POST 的 json 负载
http x
-> http://databinder.net/dispatch-doc/#dispatch.Http
>:>
-> http://databinder.net/dispatch-doc/#dispatch.HandlerVerbs
<<
、<<<
、<:<
-> http://databinder.net/dispatch-doc/#dispatch.RequestVerbs
这使用提升 JSON 来解析调用规范并在 Akka actor 中执行。
状态、标题和内容返回给调用者。
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
Lift GitHub Wiki(正在被 组装一),有一个 关于使用 Dispatch 的文章,符合您的需求。
The Lift GitHub Wiki (which is being replaced by the Assembla one), has an article on using Dispatch along the lines of what you're seeking.