Reputation: 69
Hi So I have to send json response from my Flask API including the file, so earlier I was using this for sending the json response
@app.route('/idea123',methods=['POST','GET'])
def func123():
try:
DOWNLOAD_DIRECTORY=r'../../..'
d1={}
result_file = abcd(file)
d1['Status']='Success'
d1['Created_date']=datetime.now()
#return send_from_directory(DOWNLOAD_DIRECTORY, result_file)
return d1
except Exception as e:
print(e)
d1['Status']='Failure'
d1['Created_date']=datetime.now()
return d1
Later I had to send the result file along with json response, how can i send both in same response? I have commented the file that needs to be send code
Mentioned the code above
Upvotes: 0
Views: 319
Reputation: 1
JSON is sent as a string on the network. Meanwhile, files are sent as a stream. It has a different response format, you can't send it in the same response. Alternatively, you can put your files somewhere people can download them, such as your cloud storage or your server's public directory. Then, you can put your response with the file address, so that your front-end can download it.
@app.route('/excel',methods=['GET'])
def func123():
try:
DOWNLOAD_DIRECTORY='/static/files.csv'
return {'status':'success', 'file': DOWNLOAD_DIRECTORY}
except Exception as e:
traceback.print_exc()
return {'status':'fail', 'file':''}
Upvotes: 0
Reputation: 36
If I'm not mistaken, you can use like this :
return {'d1_array': d1, 'result_json': result_file}
Upvotes: 0