Lex Lian
Lex Lian

Reputation: 585

How to print http request when using Dispatch and Scala

When I am using Dispatch library and Scala, for debugging purpose, how to print out the entire HTTP request with headers, etc. in text after writing a statement like this ?

val svc = url("http://api.hostip.info/country.php")

Upvotes: 5

Views: 2485

Answers (2)

flavian
flavian

Reputation: 28511

Dispatch is based on Netty.io, which fully implements the sl4j logging facade. The debug logging is already being done for you by:

com.ning.http.client

Beware, it logs a LOT of crap. I am assuming you are using ch.qos.logback for logging purposes:

Go to src/main/resources, create a file called default.logback.xml, and add the following to it:

<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
    </encoder>
  </appender>

  <root level="DEBUG">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

Upvotes: 8

waffle paradox
waffle paradox

Reputation: 2775

Assuming you're using the latest version of the lib, url(...) returns a Req, which is just a thin wrapper around com.ning.http.client.RequestBuilder. You can get the underlying request object with svc.toRequest, which you can then call toString on or combine the other methods available depending on what information you're really after. More info:

Java doc for Request

Source for dispatch

Upvotes: 2

Related Questions