I am trying to create an endpoint called /automated_testing. This endpoint will receive an automated POST request which will be a .txt file which contains some strings. I want to read those strings and perform some operations on it.
I am getting error :
raise exceptions.BadRequestKeyError(key) werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand. KeyError: 'files'
I am using the below code to send a request which I trigger from separate script.
import requests
with open('test.txt', 'rb') as f:
r = requests.post('http://127.0.0.1:5000/automated_testing', files={'test.txt': f})
Code for the flask server
@app.route('/automated_testing', methods=['GET','POST'])
def getfile():
if request.method == 'POST':
file = request.files['files']
a = ""
with open(file,'r') as f:
file_content = f.read()
a+=file_content
return a
else:
return "GET REQ"
return "Hi"
Content of test.txt
Hi
hello
I get that the error indicates ['files'] but I am not able to resolve the problem. Is the way I am sending the post request wrong or the flask server?
Try
file = request.files['text.txt']
or iterate over all sent files:
for file in request.files:
#do_stuff