@@ -28,30 +28,25 @@ class DHttpClientRequest implements HttpClientRequest, HttpClientResponse {
28
28
private static final String CONTENT_ENCODING = "Content-Encoding" ;
29
29
30
30
private final DHttpClientContext context ;
31
-
32
31
private final UrlBuilder url ;
33
-
34
32
private Duration requestTimeout ;
35
-
36
33
private boolean gzip ;
37
34
38
35
private BodyContent encodedRequestBody ;
39
-
40
36
private HttpRequest .BodyPublisher body ;
37
+ private String rawRequestBody ;
41
38
42
39
private HttpRequest .Builder httpRequest ;
43
40
44
41
private Map <String , List <String >> formParams ;
45
-
46
42
private Map <String , List <String >> headers ;
47
43
48
44
private boolean bodyFormEncoded ;
49
-
50
45
private long requestTimeNanos ;
51
46
52
47
private HttpResponse <?> httpResponse ;
53
-
54
48
private BodyContent encodedResponseBody ;
49
+ private boolean loggableResponseBody ;
55
50
56
51
public DHttpClientRequest (DHttpClientContext context , Duration requestTimeout ) {
57
52
this .context = context ;
@@ -127,6 +122,7 @@ public HttpClientRequest body(Object bean) {
127
122
128
123
@ Override
129
124
public HttpClientRequest body (String body ) {
125
+ this .rawRequestBody = body ;
130
126
this .body = HttpRequest .BodyPublishers .ofString (body );
131
127
return this ;
132
128
}
@@ -292,6 +288,7 @@ public HttpResponse<byte[]> asByteArray() {
292
288
293
289
@ Override
294
290
public HttpResponse <String > asString () {
291
+ loggableResponseBody = true ;
295
292
return withResponseHandler (HttpResponse .BodyHandlers .ofString ());
296
293
}
297
294
@@ -381,11 +378,11 @@ public HttpRequest request() {
381
378
public String requestBody () {
382
379
if (encodedRequestBody != null ) {
383
380
return new String (encodedRequestBody .content (), StandardCharsets .UTF_8 );
384
- }
385
- if (bodyFormEncoded ) {
381
+ } else if (bodyFormEncoded ) {
386
382
return buildEncodedFormContent ();
387
- }
388
- if (body != null ) {
383
+ } else if (rawRequestBody != null ) {
384
+ return rawRequestBody ;
385
+ } else if (body != null ) {
389
386
return body .toString ();
390
387
}
391
388
return null ;
@@ -395,6 +392,12 @@ public String requestBody() {
395
392
public String responseBody () {
396
393
if (encodedResponseBody != null ) {
397
394
return new String (encodedResponseBody .content (), StandardCharsets .UTF_8 );
395
+ } else if (httpResponse != null && loggableResponseBody ) {
396
+ String strBody = httpResponse .body ().toString ();
397
+ if (strBody .length () > 1_000 ) {
398
+ return strBody .substring (0 , 1_000 ) + "..." ;
399
+ }
400
+ return strBody ;
398
401
}
399
402
return null ;
400
403
}
0 commit comments