@@ -42,7 +42,39 @@ def fetch_input_url(cls):
4242 "request_url" : request_url ,
4343 "request_headers" : request_headers ,
4444 }
45-
45+ @classmethod
46+ def read_data_from_file (cls ):
47+ filename = input ("Enter a filename (response_data.json)" )
48+ if filename == '' :
49+ print ("filename empty, so default file (response_data.json) is used " )
50+ with open (filename , "r" ) as reader :
51+ file_content = reader .read ()
52+ try :
53+ json_data = json .loads (file_content )
54+ data = json_data .get ("data" )
55+ return data
56+ # Make sure the data is not None and send
57+ except json .JSONDecodeError :
58+ print ("Unable to parse the file, Please try again" )
59+ cls .read_data_from_file ()
60+ @classmethod
61+ def fetch_payload_data (cls ):
62+ store = int (input ("Please choose the below options? (1/2/3)" ))
63+ print ("Option 1: For sending data payload from terminal\n " )
64+ print ("Option 2: For sending data payload by reading from json file\n " )
65+ print ("Option 3: For not sending any data to POST request" )
66+ data = None
67+ if (store == 1 ):
68+ data = input ("Enter data as key value pairs" )
69+ data = eval (data )
70+ elif (store == 2 ):
71+ data = cls .read_data_from_file ()
72+ elif (store == 3 ):
73+ print (f"you have entered { store } , so sending POST request without any data" )
74+ else :
75+ print (f"You have entered { store_data } , please enter from the above options" )
76+ cls .fetch_payload_data ()
77+ return data
4678 #saves the json response into a file
4779 @classmethod
4880 def save_response_data (cls ,response_data ):
@@ -84,6 +116,20 @@ def get_request(cls):
84116 print (cls .invalid_schema_message )
85117 except Exception as exception_obj :
86118 print (exception_obj )
119+ #Make a POST request
120+ @classmethod
121+ def post_request (cls ):
122+ request_data = cls .fetch_input_url ()
123+ data = cls .fetch_payload_data ()
124+ try :
125+ response = requests .post (url = request_data ["request_url" ], headers = request_data ["request_headers" ], data = data )
126+ cls .print_response_json (response )
127+ response_data = json .loads (response .content )
128+ cls .save_response_data (response_data )
129+ except requests .exceptions .InvalidSchema :
130+ print (cls .invalid_schema_message )
131+ except Exception as exception_obj :
132+ print (exception_obj )
87133 # Make a delete request
88134 @classmethod
89135 def delete_request (cls ):
0 commit comments