How to Print Information About a Net:Httprequest for Debug Purposes

How can I print information about a NET:HTTPRequest for debug purposes?

Use set_debug_output.

http = Net::HTTP.new(url.host, url.port)
http.set_debug_output($stdout) # Logger.new("foo.log") works too

That and more in http://github.com/augustl/net-http-cheat-sheet :)

How do I see the raw HTTP request that the HttpWebRequest class sends?

You can use System.Net tracing mechanism to see the raw HTTP requests sent on the wire. You can also add your own tracelistener to the process.

How do I print the content of httprequest request?

You can print the request type using:

request.getMethod();

You can print all the headers as mentioned here:

Enumeration<String> headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
System.out.println("Header Name - " + headerName + ", Value - " + request.getHeader(headerName));
}

To print all the request params, use this:

Enumeration<String> params = request.getParameterNames(); 
while(params.hasMoreElements()){
String paramName = params.nextElement();
System.out.println("Parameter Name - "+paramName+", Value - "+request.getParameter(paramName));
}

request is the instance of HttpServletRequest

You can beautify the outputs as you desire.

How to log request/response using java.net.http.HttpClient?

If we look at jdk.internal.net.http.common.DebugLogger source code we can see a few loggers using System.Logger, which in turn will useSystem.LoggerFinder to select the logger framework. JUL is the default choice. The logger names are:

  • jdk.internal.httpclient.debug
  • jdk.internal.httpclient.websocket.debug
  • jdk.internal.httpclient.hpack.debug

They can be enabled by setting them as a system property. For example running with -Djdk.internal.httpclient.debug=true will produce:

DEBUG: [main] [147ms] HttpClientImpl(1) proxySelector is sun.net.spi.DefaultProxySelector@6dde5c8c (user-supplied=false)
DEBUG: [main] [183ms] HttpClientImpl(1) ClientImpl (async) send https://http2.github.io/ GET
DEBUG: [main] [189ms] Exchange establishing exchange for https://http2.github.io/ GET,
proxy=null
DEBUG: [main] [227ms] PlainHttpConnection(?) Initial receive buffer size is: 43690
DEBUG: [main] [237ms] PlainHttpConnection(SocketTube(1)) registering connect event
DEBUG: [HttpClient-1-SelectorManager] [239ms] SelectorAttachment Registering jdk.internal.net.http.PlainHttpConnection$ConnectEvent@354bf356 for 8 (true)
...

How to print http request when using Dispatch and Scala

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

Logging raw HTTP request/response in ASP.NET MVC & IIS7

OK, so it looks like the answer is "no you can't get the raw data, you have to reconstruct the request/response from the properties of the parsed objects". Oh well, I've done the reconstruction thing.

Using the HttpClient how can I view the request content body?

The short answer is you can't after the request has already been sent. HttpClient disposes the content body after the request is made as a convenience to the developer. See Why do HttpClient.PostAsync and PutAsync dispose the content? for some detail.

As an alternative, you could inherit from an existing HttpContent implementation, override the Dispose method, and log the body of the content prior to disposal.

Or, as suggested, use an external tool to monitor the request in flight.

Get full HTTP request

I have never used Unirest, but it uses Apache HTTP client 4, so setting debug level logger on org.apache.http should give you all the data transferred including headers and content.

Please see how to do it depending on the log system you use at

Edit: https://hc.apache.org/httpcomponents-client-4.5.x/logging.html



Related Topics



Leave a reply



Submit