5757import okhttp3 .internal .http .HttpMethod ;
5858import okhttp3 .logging .HttpLoggingInterceptor ;
5959import okhttp3 .logging .HttpLoggingInterceptor .Level ;
60+ import okio .Buffer ;
6061import okio .BufferedSink ;
6162import okio .Okio ;
6263import org .bouncycastle .jce .provider .BouncyCastleProvider ;
@@ -1339,8 +1340,15 @@ public Call buildCall(String path, String method, List<Pair> queryParams, Object
13391340 }
13401341 }
13411342 }
1343+
1344+ String contentType = headerParams .get ("Content-Type" );
1345+ // ensuring a default content type
1346+ if (contentType == null ) {
1347+ contentType = "application/json" ;
1348+ }
1349+ RequestBody requestbody = createRequestBody (method , body , formParams , contentType );
13421350
1343- callAuthenticationHeader (method , path , body , queryParams , requestHeaderMap );
1351+ callAuthenticationHeader (method , path , requestbody , queryParams , requestHeaderMap );
13441352
13451353 if (merchantConfig .isEnableClientCert ()) {
13461354 addClientCertToKeyStore ();
@@ -1357,17 +1365,27 @@ public Call buildCall(String path, String method, List<Pair> queryParams, Object
13571365
13581366
13591367 logger .info ("Request Header Parameters:\n {}" , new PrettyPrintingMap <String , String >(headerParams ));
1360- Request request = buildRequest (path , method , queryParams , body , headerParams , formParams , authNames ,
1368+ Request request = buildRequest (path , method , queryParams , requestbody , headerParams , formParams , authNames ,
13611369 progressRequestListener );
13621370 return httpClient .newCall (request );
13631371 }
1372+
1373+ private String getRequestContentSendOverNetwork (RequestBody requestBody ) throws IOException {
1374+ if (requestBody !=null ) {
1375+ Buffer buffer = new Buffer ();
1376+ requestBody .writeTo (buffer );
1377+ String payload = buffer .readUtf8 ();
1378+ return payload ;
1379+ }
1380+ return null ;
1381+ }
13641382
13651383 /*
13661384 * Purpose : This function calling the Authentication and making an Auth Header
13671385 *
13681386 */
13691387
1370- public void callAuthenticationHeader (String method , String path , Object body , List <Pair > queryParams , Map <String , String > requestHeaderMap ) {
1388+ public void callAuthenticationHeader (String method , String path , RequestBody reqBody , List <Pair > queryParams , Map <String , String > requestHeaderMap ) {
13711389
13721390 try {
13731391 String requestTarget = null ;
@@ -1399,14 +1417,7 @@ public void callAuthenticationHeader(String method, String path, Object body, Li
13991417
14001418 Authorization authorization = new Authorization ();
14011419
1402- String requestBody = null ;
1403- if ((method .equalsIgnoreCase ("POST" ) || method .equalsIgnoreCase ("PUT" ) ||
1404- method .equalsIgnoreCase ("PATCH" ))
1405- && body .equals ("{}" )) {
1406- requestBody = "{}" ;
1407- } else {
1408- requestBody = json .serialize (body );
1409- }
1420+ String requestBody = getRequestContentSendOverNetwork (reqBody );
14101421
14111422 logger .debug ("HTTP Request Body:\n " + requestBody );
14121423 boolean isMerchantDetails = merchantConfig .validateMerchantDetails (method );
@@ -1443,7 +1454,7 @@ public void callAuthenticationHeader(String method, String path, Object body, Li
14431454 requestHeaderMap .put ("v-c-client-id" , "cybs-rest-sdk-java-" + versionInfo );
14441455 }
14451456
1446- } catch (ConfigException e ) {
1457+ } catch (ConfigException | IOException e ) {
14471458 logger .error (e .getMessage ());
14481459 }
14491460
@@ -1465,7 +1476,7 @@ public void callAuthenticationHeader(String method, String path, Object body, Li
14651476 * @throws ApiException If fail to serialize the request body object
14661477 */
14671478 @ SuppressWarnings ({ "unchecked" })
1468- public Request buildRequest (String path , String method , List <Pair > queryParams , Object body ,
1479+ public Request buildRequest (String path , String method , List <Pair > queryParams , RequestBody reqBody ,
14691480 Map <String , String > headerParams , Map <String , Object > formParams , String [] authNames ,
14701481 ProgressRequestBody .ProgressRequestListener progressRequestListener ) throws ApiException {
14711482 updateParamsForAuth (authNames , queryParams , headerParams );
@@ -1474,12 +1485,20 @@ public Request buildRequest(String path, String method, List<Pair> queryParams,
14741485 final Request .Builder reqBuilder = new Request .Builder ().url (url );
14751486 processHeaderParams (headerParams , reqBuilder );
14761487
1477- String contentType = headerParams .get ("Content-Type" );
1478- // ensuring a default content type
1479- if (contentType == null ) {
1480- contentType = "application/json" ;
1488+ Request request = null ;
1489+
1490+ if (progressRequestListener != null && reqBody != null ) {
1491+ ProgressRequestBody progressRequestBody = new ProgressRequestBody (reqBody , progressRequestListener );
1492+ request = reqBuilder .method (method , progressRequestBody ).build ();
1493+ } else {
1494+ request = reqBuilder .method (method , reqBody ).build ();
14811495 }
14821496
1497+ return request ;
1498+ }
1499+
1500+ private RequestBody createRequestBody (String method , Object body , Map <String , Object > formParams ,
1501+ String contentType ) throws ApiException {
14831502 RequestBody reqBody ;
14841503 if (!HttpMethod .permitsRequestBody (method )) {
14851504 reqBody = null ;
@@ -1506,17 +1525,7 @@ public Request buildRequest(String path, String method, List<Pair> queryParams,
15061525 reqBody = serialize (body , contentType );
15071526 }
15081527 }
1509-
1510- Request request = null ;
1511-
1512- if (progressRequestListener != null && reqBody != null ) {
1513- ProgressRequestBody progressRequestBody = new ProgressRequestBody (reqBody , progressRequestListener );
1514- request = reqBuilder .method (method , progressRequestBody ).build ();
1515- } else {
1516- request = reqBuilder .method (method , reqBody ).build ();
1517- }
1518-
1519- return request ;
1528+ return reqBody ;
15201529 }
15211530
15221531 /**
0 commit comments