0

I'm using spray client's request level api to request a server.

This is my code:

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

I'm not getting response as expected. The execution context is going to Failure. I'm receiving ChunkedResponseStart(HttpResponse(200 OK,Empty,List(Transfer-Encoding: chunked) instead of HttpResponse itself and mapTo is failing.

I'm not sure why this is happening and how to resolve the same.

Yoda
  • 442
  • 3
  • 14

1 Answers1

0

I think it will help to you http://spray.io/documentation/1.2.2/spray-client/

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

you can write something like this

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

and extract

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

to get info you can use

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

and

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