10
10
import java .util .Map ;
11
11
12
12
import org .apache .http .Header ;
13
+ import org .apache .http .HttpMessage ;
13
14
import org .apache .http .annotation .NotThreadSafe ;
14
15
import org .apache .http .client .ClientProtocolException ;
15
16
import org .apache .http .client .ResponseHandler ;
@@ -201,9 +202,7 @@ public Response post(Request request) throws URISyntaxException, IOException {
201
202
}
202
203
203
204
httpPost .setEntity (new StringEntity (request .getBody (), Charset .forName ("UTF-8" )));
204
- if (request .getBody () != "" ) {
205
- httpPost .setHeader ("Content-Type" , "application/json" );
206
- }
205
+ writeContentTypeIfNeeded (request , httpPost );
207
206
208
207
return executeApiCall (httpPost );
209
208
}
@@ -230,9 +229,8 @@ public Response patch(Request request) throws URISyntaxException, IOException {
230
229
}
231
230
232
231
httpPatch .setEntity (new StringEntity (request .getBody (), Charset .forName ("UTF-8" )));
233
- if (request .getBody () != "" ) {
234
- httpPatch .setHeader ("Content-Type" , "application/json" );
235
- }
232
+ writeContentTypeIfNeeded (request , httpPatch );
233
+
236
234
return executeApiCall (httpPatch );
237
235
}
238
236
@@ -258,9 +256,7 @@ public Response put(Request request) throws URISyntaxException, IOException {
258
256
}
259
257
260
258
httpPut .setEntity (new StringEntity (request .getBody (), Charset .forName ("UTF-8" )));
261
- if (request .getBody () != "" ) {
262
- httpPut .setHeader ("Content-Type" , "application/json" );
263
- }
259
+ writeContentTypeIfNeeded (request , httpPut );
264
260
265
261
return executeApiCall (httpPut );
266
262
}
@@ -286,13 +282,17 @@ public Response delete(Request request) throws URISyntaxException, IOException {
286
282
}
287
283
288
284
httpDelete .setEntity (new StringEntity (request .getBody (), Charset .forName ("UTF-8" )));
289
- if (request .getBody () != "" ) {
290
- httpDelete .setHeader ("Content-Type" , "application/json" );
291
- }
285
+ writeContentTypeIfNeeded (request , httpDelete );
292
286
293
287
return executeApiCall (httpDelete );
294
288
}
295
289
290
+ private void writeContentTypeIfNeeded (Request request , HttpMessage httpMessage ) {
291
+ if (!"" .equals (request .getBody ())) {
292
+ httpMessage .setHeader ("Content-Type" , "application/json" );
293
+ }
294
+ }
295
+
296
296
private Response executeApiCall (HttpRequestBase httpPost ) throws IOException {
297
297
try {
298
298
CloseableHttpResponse serverResponse = httpClient .execute (httpPost );
0 commit comments