How to avoid sending a chunked response?

3,281 views
Skip to first unread message

David Walend

unread,
Sep 15, 2015, 4:08:42 PM9/15/15
to spray.io User List

It's mostly working - the response logged on the server side looks like it has the right contents. However, it's header says it's chunked ("Transfer-Encoding: chunked" in the header), and curl is choking on it ("curl: (56) Illegal or missing hexadecimal sequence in chunked-encoding") .

These messages are tiny, and just bouncing around localhost. I've no need for chunking. (It's quite possible the old system is chunking things, but curl doesn't complain about the missing bits.)

What do I need to do to make the response not chunked? What should I read next?

Thanks,

David



David Walend

unread,
Sep 17, 2015, 4:47:34 PM9/17/15
to spray.io User List
I found my own somewhat uninspired fix - I just build my own HttpRequest, use that, then scoop out the HttpReply's entity and complete with that.

I'd still like to understand why it's getting chunked, though.

Here's the code snippet I came up with. Let me know if something is out of joint: 

import spray.can.Http
import akka.io.IO
import akka.actor.{ActorRef, ActorSystem}
import spray.http.{HttpResponse, HttpRequest, Uri}
import spray.routing.{Route, RequestContext}
import akka.pattern.ask

import scala.concurrent.duration.DurationInt
import scala.util.Try

import scala.concurrent.ExecutionContext.Implicits.global

/**
 * Replace when Spray has its own version.
 *
 * @author david
 * @since 9/14/15
 */
trait HttpClientDirectives extends Loggable {

  private def sending(f: RequestContext ⇒ HttpRequest)(implicit system: ActorSystem): Route = {
    val transport: ActorRef = IO(Http)(system)
    ctx ⇒ {
      val request = f(ctx)
      debug(s"Forwarding request to happy service $request")
      import scala.concurrent.blocking
      blocking {
        transport.ask(request)(10 seconds).onComplete { tryAny: Try[Any] =>
//todo replace with fold when try gets it in scala 2.12
          val any = tryAny.get
          any match {
            case response: HttpResponse => ctx.complete(response.entity)
          }
        }
      }
    }
  }

  /**
   * proxy the request to the specified uri
   *
   */
  def httpRequest(uri: Uri)(implicit system: ActorSystem): Route = {
    sending { ctx =>
      HttpRequest(ctx.request.method,
        uri.withPath(uri.path.++(ctx.unmatchedPath)))
    }
  }

  /**
   * proxy the request to the specified uri with the unmatched path
   *
   */
  def httpRequestWithUnmatchedPath(uri: Uri)(implicit system: ActorSystem): Route = {
    sending { ctx ⇒
      HttpRequest(ctx.request.method,
                  uri.withPath(uri.path.++(ctx.unmatchedPath)))
    }
  }
}

object HttpClientDirectives extends HttpClientDirectives

Thanks,

David
Reply all
Reply to author
Forward
0 new messages