scalaj-http
scalaj-http copied to clipboard
Printing HttpRequests?
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?
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 ...
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 ?
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.
Hi folks, is this issue fixed now?
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
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