【问题标题】:How to use Play2 HTTP client's methods that take Iteratees?如何使用采用 Iteratees 的 Play2 HTTP 客户端方法?
【发布时间】:2013-08-22 09:54:40
【问题描述】:

我打算将 Iteratees 与 Play2 HTTP client's methods 一起使用。 Play2 的docs 相当复杂。

采用这些 Play2 方法(GET method):

 /**
 * performs a GET with supplied body
 * @param consumer that's handling the response
 */
 def get[A](consumer: ResponseHeaders => Iteratee[Array[Byte], A]):
   Future[Iteratee[Array[Byte], A]] =
     prepare("GET").executeStream(consumer)

还有这个PUT method:

 /**
 * performs a PUT with supplied body
 * @param consumer that's handling the response
 */
 def putAndRetrieveStream[A, T](body: T)
   (consumer: ResponseHeaders => Iteratee[Array[Byte], A])
     (implicit wrt: Writeable[T], ct: ContentTypeOf[T]):
       Future[Iteratee[Array[Byte], A]] =
         prepare("PUT", body).executeStream(consumer)

我如何调用这个get 方法,以便我得到响应的主体为Array[Byte]?以及如何调用putAndRetrieveStream 方法,以便它在请求正文中发送给定的Array[Byte]

【问题讨论】:

    标签: http scala stream playframework-2.0


    【解决方案1】:

    您希望您的结果为Array[Byte],因此您需要创建一个Iteratee[Array[Byte],Array[Byte]]

    val resp  = req.get((r:ResponseHeaders => Iteratee.consume[Array[Byte]]()))
    resp onComplete {
        case Success(iter) => iter match {
            case Done(bytes,rem) => do_something_with_bytearray(bytes)       
        }
        case Failure(t) => do_something(t)
    }
    

    同样:

    val resp = req.putAndRetrieveStream(data)((r:ResponseHeaders => Iteratee.consume[Array[Byte]]()))
    

    其中 data 是您要与 put 请求一起发送的数据。可以是字符串或字节数组等。

    注意:我没有尝试过这段代码,但这会给你正确的方向。

    【讨论】:

      猜你喜欢
      • 2014-05-24
      • 2020-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多