@@ -148,6 +148,10 @@ public abstract static class RequestBody {
148
148
149
149
public abstract String getContentType () throws IOException ;
150
150
151
+ protected ContentType entityContentType () throws IOException {
152
+ return this .getCharset () != null ? ContentType .create (this .getContentType (), this .getCharset ()) : ContentType .create (this .getContentType ());
153
+ }
154
+
151
155
public static RequestBody from (HttpEntity entity ) throws IOException {
152
156
if (entity == null ) {
153
157
return null ;
@@ -193,13 +197,12 @@ public static class InputStreamRequestBody extends RequestBody {
193
197
@ Builder .Default
194
198
private String contentType = ContentType .APPLICATION_OCTET_STREAM .getMimeType ();
195
199
196
- @ Builder .Default
197
- private Charset charset = StandardCharsets .UTF_8 ;
200
+ private Charset charset ;
198
201
199
202
private InputStream content ;
200
203
201
- public HttpEntity to () {
202
- return new InputStreamEntity (content , ContentType . create ( contentType , charset ));
204
+ public HttpEntity to () throws IOException {
205
+ return new InputStreamEntity (content , this . entityContentType ( ));
203
206
}
204
207
}
205
208
@@ -210,13 +213,12 @@ public static class StringRequestBody extends RequestBody {
210
213
@ Builder .Default
211
214
private String contentType = ContentType .TEXT_PLAIN .getMimeType ();
212
215
213
- @ Builder .Default
214
- private Charset charset = StandardCharsets .UTF_8 ;
216
+ private Charset charset ;
215
217
216
218
private String content ;
217
219
218
- public HttpEntity to () {
219
- return new StringEntity (this .content , ContentType . create ( contentType , charset ));
220
+ public HttpEntity to () throws IOException {
221
+ return new StringEntity (this .content , this . entityContentType ( ));
220
222
}
221
223
}
222
224
@@ -227,22 +229,20 @@ public static class ByteArrayRequestBody extends RequestBody {
227
229
@ Builder .Default
228
230
private String contentType = ContentType .APPLICATION_OCTET_STREAM .getMimeType ();
229
231
230
- @ Builder .Default
231
- private Charset charset = StandardCharsets .UTF_8 ;
232
+ private Charset charset ;
232
233
233
234
private byte [] content ;
234
235
235
- public HttpEntity to () {
236
- return new ByteArrayEntity (content , ContentType . create ( contentType , charset ));
236
+ public HttpEntity to () throws IOException {
237
+ return new ByteArrayEntity (content , this . entityContentType ( ));
237
238
}
238
239
}
239
240
240
241
@ Getter
241
242
@ AllArgsConstructor
242
243
@ SuperBuilder
243
244
public static class JsonRequestBody extends RequestBody {
244
- @ Builder .Default
245
- private Charset charset = StandardCharsets .UTF_8 ;
245
+ private Charset charset ;
246
246
247
247
private Object content ;
248
248
@@ -255,7 +255,7 @@ public HttpEntity to() throws IOException {
255
255
try {
256
256
return new StringEntity (
257
257
JacksonMapper .ofJson ().writeValueAsString (content ),
258
- ContentType .APPLICATION_JSON .withCharset (this .charset )
258
+ this . charset != null ? ContentType .APPLICATION_JSON .withCharset (this .charset ) : ContentType . APPLICATION_JSON
259
259
);
260
260
} catch (JsonProcessingException e ) {
261
261
throw new IOException (e );
@@ -267,8 +267,7 @@ public HttpEntity to() throws IOException {
267
267
@ AllArgsConstructor
268
268
@ SuperBuilder
269
269
public static class UrlEncodedRequestBody extends RequestBody {
270
- @ Builder .Default
271
- private Charset charset = StandardCharsets .UTF_8 ;
270
+ private Charset charset ;
272
271
273
272
private Map <String , Object > content ;
274
273
@@ -278,22 +277,20 @@ public String getContentType() throws IOException {
278
277
}
279
278
280
279
public HttpEntity to () throws IOException {
281
- return new UrlEncodedFormEntity (
282
- this .content .entrySet ()
283
- .stream ()
284
- .map (e -> new BasicNameValuePair (e .getKey (), e .getValue ().toString ()))
285
- .toList (),
286
- this .charset
287
- );
280
+ List <BasicNameValuePair > list = this .content .entrySet ()
281
+ .stream ()
282
+ .map (e -> new BasicNameValuePair (e .getKey (), e .getValue ().toString ()))
283
+ .toList ();
284
+
285
+ return this .charset != null ? new UrlEncodedFormEntity (list , this .charset ) : new UrlEncodedFormEntity (list );
288
286
}
289
287
}
290
288
291
289
@ Getter
292
290
@ AllArgsConstructor
293
291
@ SuperBuilder
294
292
public static class MultipartRequestBody extends RequestBody {
295
- @ Builder .Default
296
- private Charset charset = StandardCharsets .UTF_8 ;
293
+ private Charset charset ;
297
294
298
295
private Map <String , Object > content ;
299
296
@@ -304,8 +301,11 @@ public String getContentType() throws IOException {
304
301
305
302
public HttpEntity to () throws IOException {
306
303
MultipartEntityBuilder builder = MultipartEntityBuilder
307
- .create ()
308
- .setCharset (this .charset );
304
+ .create ();
305
+
306
+ if (this .charset != null ) {
307
+ builder .setCharset (this .charset );
308
+ }
309
309
310
310
content .forEach ((key , value ) -> {
311
311
switch (value ) {
0 commit comments