javaresthttpunirest

Get full HTTP request


For debugging matters, I would like to get the request executed by Unirest-Java when presented with a set of options. How can I get this:

POST / HTTP/1.1
Host: www.some.host.tld
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
field1=FIELD1&field2=FIELD2

From this:

HttpRequest request = Unirest.post(SOMEHOST_URL)
            .field("field1" , FIELD1)
            .field("field2", FIELD2)
            .getHttpRequest();

That is, how can I get the full HTTP request from a HttpRequest? I don't really know of a proxy to use, as the only one with which I could get SSL support working was Charles, and for some reason it won't pickup the java traffic. Other tools would choke on the SSL mainly because the server I need to talk to is flawed, using self-signed certificates and invalid hostnames. So I would gladly try a proxy, but it has to work under these conditions. Better would be to extract this information from Unirest/HTTPClient itself. I already tried building the HttpClient with .setInterceptorFirst( but I couldn't get the request body from the interceptor, only some of its headers.


Solution

  • 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