scalaj-http icon indicating copy to clipboard operation
scalaj-http copied to clipboard

Printing HttpRequests?

Open heathermiller opened this issue 9 years ago • 6 comments

Is there any way to print the full HttpRequest for the sake of debugging? I'm trying to figure out why a request that I have that works fine in curl doesn't work in scalaj-http. In particular, I wanted to see what headers etc scalaj adds so I can compare with my verbose curl output.

In an old issue I see that you've used the asString method. But this appears to be pre-v2, and now when you call asString, in addition to executing, it prints something a lot less helpful. In my case:

HttpRequest(http://www.myurl.org,POST,<function2>,List(),List((User-Agent,scalaj-http/1.0), (Host,www.myurl.org), (Accept,*/*), (Cache-Control,no-cache), (Content-Type,application/json)),List(<function1>, <function1>, <function1>),None,UTF-8,4096,<function1>,true)

Any tips for coaxing out the full request?

heathermiller avatar Feb 16 '16 20:02 heathermiller

Hi,

As you've noticed, the HttpRequest isn't an exact analogue of the http request sent over the wire, it's just a wrapper for some client state. Currently, there's no way to print out exactly what is sent over the wire like curl does, but you can execute a request to an echo service like httpbin.org. Does that help?

scala> Http("http://httpbin.org/post").postData("{\"message\":\"hi\"}").header("content-type", "application/json").asString
res4: scalaj.http.HttpResponse[String] = 
HttpResponse({
  "args": {}, 
  "data": "{\"message\":\"hi\"}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2", 
    "Accept-Encoding": "gzip,deflate", 
    "Content-Length": "16", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "scalaj-http/1.0"
  }, 
  "json": {
    "message": "hi"
  }, 
  "origin": "207.110.15.216", 
  "url": "http://httpbin.org/post"
}
,200,Map(Access-Control-Allow-Credentials -> Vector(true), Access-Control-Allow-Origin -> Vector(*), Connection -> Vector(keep-alive), Content-Length -> Vector(455), Content-Type -> Vector(application/json), Date -> Vector(Tue, 16 Feb 2016 21:30:39 GMT), Server -> Vector(nginx), Status ...

hoffrocket avatar Feb 16 '16 21:02 hoffrocket

I faced a similar need a couple days ago and would be willing to try and make a patch for this. Is this something you think a good idea @hoffrocket ?

helderasmoreira avatar Feb 19 '16 01:02 helderasmoreira

That would be great. I think challenge is that this library is just a wrapper around java.net.HttpURLConnection and it might be difficult to see what's actually sent over the wire since that JRE library adds/updates/omits items in the http request header. Any ideas are very welcome though.

hoffrocket avatar Feb 19 '16 12:02 hoffrocket

Hi folks, is this issue fixed now?

shivagopalan avatar Aug 22 '17 20:08 shivagopalan

Hi any updates? Or any work-around for printing just the URL with all the parameters added in? That would satisfy 90% of my debugging items, the headers aren't as essential. Thanks

alexbohr avatar Nov 12 '17 18:11 alexbohr

Hi, as scalaj-http is using HttpUrlConnection under the hood, it's possible to enable logs for HttpUrlConnection and see what is being sent over the wire. This link in stack overflow has a good explanation on how to enable it:

https://stackoverflow.com/questions/1445919/how-to-enable-wire-logging-for-a-java-httpurlconnection-traffic

Hope it helps

alexandredantas avatar Dec 07 '18 13:12 alexandredantas