我正在尝试将csv文件作为一个服务来开发,这样就可以将csv文件发送到我的REST中,我将该API转储到数据库中。我的cURL请求正在读取数据,但flask_restful无法处理它。你能告诉我我做错了什么吗?我该怎么解决呢?
在下面编辑
在阅读了文档之后,我发现request.files允许您从表单中读取来自POST请求的文件。我还找到了一种通过cURL作为表单发送csv文件的方法。
class ForBetaAndUpload(Resource):
def post(self, kind='quotes'):
# parser = reqparse.RequestParser()
file = request.files['file']
print(file)
# kind = parser.add_argument('kind').parse_args()['kind']
if kind:
if file and file[-3:]=='csv':
if kind == 'quotes':
try:
df = pd.read_csv(file)
df.to_sql('QUOTES', helper.conx, index=False, if_exists='append')
return jsonify({'message':'%s rows inserted in the databasee table successfully' %(df.shape[0])})
except Exception as e:
return jsonify({'exception': e})
if kind == 'trace':
try:
df = pd.read_csv(todos)
df.iloc[:10].to_sql('TRACE', helper.conx, index=False, if_exists='append')
return jsonify({'message':'%s rows inserted in the databasee table successfully' %(df.shape[0])})
except Exception as e:
return jsonify({'message': e})
else:
return jsonify({'message': 'Please provide a csv file'})
else:
return jsonify({'message':'Please provide the file for to update relevant table in the databse'})
api.add_resource(ForBetaAndUpload, '/upload', endpoint='upload')
if __name__ == "__main__":
app.run(debug=True)cURL请求:
curl "https://localhost:5000/upload" -X POST -H 'Content-Type: txt/csv' -d trace.csv --insecure我收到了以下消息:
curl:(35)错误:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown协议
API错误消息
代码400,消息Bad /0.9请求类型为('\x16\x03\x01\x02\x00\x01\x00\x01ü\x03\x03\x08Ú:ü^¢Ù~ö7W\x9fDyy\x16j\x7fõ>½\x82\x90uÎ&3ÿZ\x08êE\x00\x00')
如何将csv文件发送到flask restful_api。我所做的是对的,还是有其他的方法去做?
发布于 2018-08-14 19:36:41
我从Flask读取csv的解决方案是:
在水瓶中:
f = request.files['file']f将是文件的处理程序,然后可以使用csv_reader。
并发送文件:
curl -s "http://localhost:5000" \
-F file=@./myfile.csv \
-X POST \
-H 'enctype:multipart/form-data ; Content-Type:multipart/form-data'这应该管用。让我知道。
https://stackoverflow.com/questions/50997532
复制相似问题