I’m getting three different results which is in json format and want to reflect the result on API. I’m using Flask to show the post request. This should be done by payloads. So, these three json have three different Urls. Earlier I was using hard coded paths to getting the results. Now, new instruction is, dynamically I’ve to get the paths of all the required data and show the results.
Below code I’m using to show the post request and the main_func() from where I’m getting data. Whenever I’m hitting the code it is showing error – "Request’ object has no attribute ‘body". I’m bit confused here, to get the result as dynamically.
@app.route('/result_spec', methods=['POST'])
def get_result_spec():
try:
payload = get_params_api(request)
result_spec, _, _ = main_func(request = payload)
return jsonify({"result_spec": result_spec})
except Exception as e:
return jsonify({"error": str(e)}), 500
def get_params_api(request):
# if request and hasattr(request, 'body'):
if request is not None:
json_body = json.loads(request.body)
# if json_body is not None:
ads_path = json_body["csv_path"]
json_path = json_body["spec_path"]
input_path = json_body["input_path"]
variable = json_body["variable"]
files_ = json_body["files_"]
variable_name1 = json_body["variable_name1"]
t_and_v = json_body["t_and_v"]
else:
csv_path = r"file.csv"
input_path = r"split_folder"
json_path = r"sample_transformation.json"
variable, files_, variable_name1, t_and_v = get_params()
payload = {
"csv_path": csv_path,
"json_path": json_path,
"input_path": input_path,
"dependent_variable": dependent_variable,
"files_geo": files_geo,
"variable_name": variable_name,
"trans_and_values": trans_and_values
}
return payload
def main_func(params):
csv_path = params["csv_path"]
json_path = params["json_path"]
input_path = params["input_path"]
variable = params["variable"]
files_ = params["files_"]
variable_name1 = params["variable_name1"]
t_and_v = params["t_and_v"]
# store_path_list = get_s3_directory_tree(input_path)
spath_list = get_s3_directory_tree(input_path)
df_data = get_S3_csv_data(spath_list[0])
header_ = get_header_names(df_data)
sp = read_json(json_path, header_)
result_spec = None
result_json = None
if user_specific_geo:
# result_spec, result_json, result_json_const = main_pipeline_user_specific_copy(spec, files, input_path, files_geo, dependent_variable, variable_name, trans_and_values)
result_spec, result_json, result_json_const = main_pipeline_user_specific(spec,store_path_list, files_geo, dependent_variable, variable_name, trans_and_values)
else:
result_spec, result_json, result_json_const = main_pipeline(spec, dependent_variable,store_path_list)
# payload_data = {"result_spec" : result_spec, "result_json" : result_json,
# "result_json_const" : result_json_const}
return result_spec, result_json , result_json_const
if __name__ == "__main__":
payload = get_params_api(request=None)
result_spec,result_json_const, result_json = main_func(params=payload)
2
Answers
The request object actually has no body attribute. To request data in json format without the help of the json package, you can use the
get_json()
function of the request object.To access data passed to a POST request you can use any of below.
or
requests object don’t have body.
For /GET request you can use request.args.get(‘csv_path’)
Also you can Optimize code by removing get_params_api() function and access necessary parameters in direct main_func() like upper example.