66
77
88class DataPreparation ():
9+
10+ # Function to base64 encode data
11+ def base64_encode (self , data ):
12+ try :
13+ if global_variables .DEBUG :
14+ print ("about to parse request body as base64" )
15+ encoded_body = base64 .standard_b64encode (data )
16+ transfer_encoding = 'base64'
17+ if global_variables .DEBUG :
18+ print ("base64 encoded body: " + encoded_body )
19+ except :
20+ if global_variables .DEBUG :
21+ print ("Request body is of type other than json or base64" )
22+ encoded_body = None
23+ transfer_encoding = None
24+
25+ return encoded_body , transfer_encoding
26+
927 # Function to prepare the data
1028 def transform_data (self , method , url , start_time , end_time , response , kwargs ):
1129
@@ -20,25 +38,25 @@ def transform_data(self, method, url, start_time, end_time, response, kwargs):
2038 if kwargs .get ('data' , None ):
2139 try :
2240 if global_variables .DEBUG :
23- print ("about to parse request body as json" )
24- req_body = json .loads (kwargs .get ('data' , None ))
41+ print ("about to parse request data as json" )
42+ req_body = json .loads (json . dumps ( kwargs .get ('data' , None ) ))
2543 if global_variables .DEBUG :
26- print ("Req body json parsed succesfully " )
44+ print ("Req body data parsed successfully " )
2745 req_body = utility_function .mask_body (req_body , global_variables .moesif_options .get ('REQUEST_BODY_MASKS' ))
2846 req_body_transfer_encoding = 'json'
2947 except :
30- try :
31- if global_variables . DEBUG :
32- print ( "about to parse request body as base64" )
33- req_body = base64 . standard_b64encode ( kwargs . get ( 'data' , None ))
34- req_body_transfer_encoding = 'base64'
35- if global_variables . DEBUG :
36- print ( "base64 encoded body: " + req_body )
37- except :
38- if global_variables .DEBUG :
39- print ( "Request body is of type other than json or base64" )
40- req_body = None
41- req_body_transfer_encoding = None
48+ req_body , req_body_transfer_encoding = self . base64_encode ( json . dumps ( kwargs . get ( 'data' , None )))
49+ elif kwargs . get ( 'json' , None ) :
50+ try :
51+ if global_variables . DEBUG :
52+ print ( 'about to parse request json' )
53+ req_body = json . loads ( kwargs . get ( 'json' , None ))
54+ if global_variables . DEBUG :
55+ print ( "Req body json parsed successfully" )
56+ req_body = utility_function . mask_body ( req_body , global_variables .moesif_options . get ( 'REQUEST_BODY_MASKS' ))
57+ req_body_transfer_encoding = ' json'
58+ except :
59+ req_body , req_body_transfer_encoding = self . base64_encode ( kwargs . get ( 'json' , None ))
4260 else :
4361 req_body = None
4462 req_body_transfer_encoding = None
0 commit comments