Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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")
like image 884
Lex Lian Avatar asked Oct 19 '13 09:10

Lex Lian


2 Answers

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>
like image 103
flavian Avatar answered Oct 28 '22 04:10

flavian


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

like image 2
waffle paradox Avatar answered Oct 28 '22 06:10

waffle paradox