【问题标题】:Handling http response in request level api in spray client在喷雾客户端的请求级 api 中处理 http 响应
【发布时间】:2015-04-24 14:34:10
【问题描述】:

我正在使用喷雾客户端的请求级别 api 来请求服务器。

这是我的代码:

  var request: HttpRequest = HttpRequest(method, uri, headers, HttpEntity(MediaTypes.`application/json`, json.toString()))
  val result = for {
   resp <- io.ask(request).mapTo[HttpResponse]
  } yield {
    println("Response:" + resp)
  }
  result onComplete {
     case Success(value) => println("success" + value)
     case Failure(ex)    => println("failure" + ex)
  }

我没有得到预期的响应。执行上下文将失败。我收到的是 ChunkedResponseStart(HttpResponse(200 OK,Empty,List(Transfer-Encoding: chunked) 而不是 HttpResponse 本身,并且 mapTo 失败了。

我不确定为什么会发生这种情况以及如何解决。

【问题讨论】:

    标签: scala spray-client


    【解决方案1】:

    我认为它会对你有所帮助 http://spray.io/documentation/1.2.2/spray-client/

    val 响应:Future[HttpResponse] = pipeline(Get("http://spray.io/"))

    你可以这样写

    val req: Future[HttpResponse] = pipeline(Post("http://localhost:xxxx/...")
            .withHeaders(RawHeader("Name", param))
            .withEntity(HttpEntity("application/json", data.toJson.toString())))
    

    并提取

    def extract(req: Future[HttpResponse]): HttpResponse = Await.result(req, 40.seconds)
    

    获取您可以使用的信息

    def getStatusCode(response: HttpResponse) = response.status.intValue
    

    if(getStatusCode(response) == 200) println(~better use same test lib or match~)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-14
      • 1970-01-01
      • 2018-04-29
      • 2020-12-16
      相关资源
      最近更新 更多