If you are using Java 11’s HttpClient
and want to log request and response details, you can achieve this by implementing a custom utility. Java 11’s HttpClient
provides flexibility to log and inspect both requests and responses using its APIs. Here’s how you can log them:
Full Implementation for Logging Request and Response
Below is an example that demonstrates logging details such as HTTP headers, request body, response status, response headers, and response body.
package org.kodejava.net.http;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
public class HttpClientLogger {
public static void main(String[] args) {
try {
// Create a sample HTTP GET request
HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://jsonplaceholder.typicode.com/posts/1"))
.GET()
.header("Accept", "application/json")
.timeout(Duration.ofSeconds(10))
.build();
// Log Request Details
logRequestDetails(request);
// Send the request and receive a response
HttpResponse<String> response = httpClient.send(request,
HttpResponse.BodyHandlers.ofString());
// Log Response Details
logResponseDetails(response);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void logRequestDetails(HttpRequest request) {
System.out.println("---- HTTP Request Details ----");
System.out.println("Method: " + request.method());
System.out.println("URI: " + request.uri());
System.out.println("Headers: " + request.headers().map());
request.bodyPublisher().ifPresentOrElse(
bodyPublisher -> {
System.out.println("Request Body: (BodyPublisher details not directly accessible, consider passing it explicitly)");
},
() -> System.out.println("Request Body: No body")
);
System.out.println("-------------------------------");
}
private static void logResponseDetails(HttpResponse<String> response) {
System.out.println("---- HTTP Response Details ----");
System.out.println("Status Code: " + response.statusCode());
System.out.println("Headers: " + response.headers().map());
System.out.println("Body: " + response.body());
System.out.println("--------------------------------");
}
}
Explanation of the Code
- Setting up
HttpClient
:- We create a
HttpClient
instance usingHttpClient.newHttpClient()
.
- We create a
- Building the
HttpRequest
:- Use the
HttpRequest.Builder
to construct the request. This includes setting the URI, method, headers, and a timeout.
- Use the
- Logging HTTP Request Details:
- Log details of the request by accessing:
- HTTP Method:
HttpRequest::method
- URI:
HttpRequest::uri
- Headers:
HttpRequest::headers
- The request body can be logged if you manually supply it during the request creation since
BodyPublisher
doesn’t provide a content preview.
- HTTP Method:
- Log details of the request by accessing:
- Sending the Request:
- Use
HttpClient::send
to perform the HTTP operation, and specifyHttpResponse.BodyHandlers.ofString()
to read the response as a string.
- Use
- Logging HTTP Response Details:
- Log response information:
- Status Code:
HttpResponse::statusCode
- Headers:
HttpResponse::headers
- Body:
HttpResponse::body
- Status Code:
- Log response information:
Notes
- POST/PUT Requests with a Body:
- If you are sending POST or PUT requests that include a body (e.g., JSON or a form), you should explicitly log the body content when building the request. Example:
HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://jsonplaceholder.typicode.com/posts")) .POST(HttpRequest.BodyPublishers.ofString("{\"key\":\"value\"}")) .header("Content-Type", "application/json") .build();
To log the request body, simply store it in a separate variable and print it in
logRequestDetails
.
- If you are sending POST or PUT requests that include a body (e.g., JSON or a form), you should explicitly log the body content when building the request. Example:
-
Production Logging:
- Avoid directly printing details to the console in production.
- Use proper logging libraries like SLF4J with an implementation (e.g., Logback or Log4j) to write logs at different levels like DEBUG, INFO, ERROR, etc.
- Sensitive Data:
- Avoid logging sensitive details like authentication headers or personal data (e.g., Authorization tokens, passwords).
This approach provides a reusable template for logging HTTP requests and responses while using the Java 11+ HttpClient
.