如何使用scala调度库向Lift中的服务器发送post请求?

发布于 2024-08-19 00:57:40 字数 105 浏览 6 评论 0原文

我想使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴意少年 2024-08-26 00:57:40

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.

只为一人 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 中执行。
状态、标题和内容返回给调用者。

Here is a snippet that dispatches REST calls to a server:

    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)
      }
    }

Where:

event.call -> json specifying the call

event.payload -> json payload for PUT and POST

http x -> http://databinder.net/dispatch-doc/#dispatch.Http

>:> -> http://databinder.net/dispatch-doc/#dispatch.HandlerVerbs

<< , <<< , <:< -> http://databinder.net/dispatch-doc/#dispatch.RequestVerbs

This uses Lift JSON for parsing the call spec and executes in a Akka actor.
Status, headers and content is returned to the calling actor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文