首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用cURL将CSV文件读取到烧瓶Restful

使用cURL将CSV文件读取到烧瓶Restful
EN

Stack Overflow用户
提问于 2018-06-23 02:47:01
回答 1查看 2.6K关注 0票数 1

我正在尝试将csv文件作为一个服务来开发,这样就可以将csv文件发送到我的REST中,我将该API转储到数据库中。我的cURL请求正在读取数据,但flask_restful无法处理它。你能告诉我我做错了什么吗?我该怎么解决呢?

下面编辑

在阅读了文档之后,我发现request.files允许您从表单中读取来自POST请求的文件。我还找到了一种通过cURL作为表单发送csv文件的方法。

代码语言:javascript
复制
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请求:

代码语言:javascript
复制
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。我所做的是对的,还是有其他的方法去做?

EN

回答 1

Stack Overflow用户

发布于 2018-08-14 19:36:41

我从Flask读取csv的解决方案是:

在水瓶中:

代码语言:javascript
复制
f = request.files['file']

f将是文件的处理程序,然后可以使用csv_reader

并发送文件:

代码语言:javascript
复制
curl -s "http://localhost:5000" \
    -F file=@./myfile.csv \
    -X POST \
    -H 'enctype:multipart/form-data ; Content-Type:multipart/form-data'

这应该管用。让我知道。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50997532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档