I have created a flask api and it is currently running in local machine From one machine I ran the python script to post the text file:
import json
import requests
datas = {'var1' : 'var1','var2' : 'var2',}
#my file to be sent
local_file_to_send = 'C:\\Users\\tmp.txt'
with open(local_file_to_send, 'w') as f:
f.write('I am a file\n')
url = "http://127.0.0.1:5000/customerupdate"
files = [
('document', (local_file_to_send, open(local_file_to_send, 'rb'), 'application/octet')),
('datas', ('datas', json.dumps(datas), 'application/json')),
]
r = requests.post(url, files=files)
print("sd")
print(str(r.content, 'utf-8'))
Python flask API:
import json
from flask import Flask, request
from flask import send_file
app = Flask(__name__)
@app.route('/',methods=['GET'])
def hello_world():
return 'Hello World!'
@app.route('/customerupdate',methods=['GET','POST'])
def customerupdate():
posted_file = str(request.files['document'].read(), 'utf-8')
posted_data = json.load(request.files['datas'])
print(posted_file)
print(posted_data)
return '{}\n{}\n'.format(posted_file, posted_data)
if __name__=='__main__':
app.run(debug='true',threaded=True)
And the python get method to receive the file:
import requests
r = requests.get('http://127.0.0.1:5000')
print(r.text)
But here in get method I have received the contents of the file.But I want it for csv file.I tried by making that .csv instead of .txt.But can't able to send the file to api .So, Is it possible to send the csv file from one machine and receive the csv file in another machine via flask and please post the code if solutions available. Already tried with this link:Python Flask: Send file and variable
I struggling lot to resolve it.