1

I am trying to develop as a service where someone can send the csv file to my REST API which I dump in the database. My cURL request is reading the data but flask_restful is not able to process it. Can you please tell me what am I doing wrong and how can I fix it?

[EDITED BELOW]

I found after reading the docs that request.files lets you read the files from the POST request from a form. I also found a way to send a csv file through cURL as a form.

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 Request:

curl "https://localhost:5000/upload" -X POST -H 'Content-Type: txt/csv' -d trace.csv --insecure

I'm getting the following message:

curl: (35) error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

API Error Message

code 400, message Bad HTTP/0.9 request type ('\x16\x03\x01\x02\x00\x01\x00\x01ü\x03\x03\x08Ú:ü^¢Ù~ö7W\x9fDyy\x16j\x7fõ>½\x82\x90uÎ&3ÿZ\x08êE\x00\x00')

How can I send a csv file to the flask restful_api. Is it right what I am doing or is there any other way to do it?

7
  • try to use curl -k for bypass SSL cert Commented Jun 23, 2018 at 4:39
  • @ThanhNguyenVan: I tried curl -K "https://localhost:5000/upload" -X POST -H 'Content-Type: text/csv; charset=utf-8' -d @trace.csv --insecure and now I'm getting Warning: error trying read config from the 'localhost:5000/upload' Warning: file curl: no URL specified! curl: try 'curl --help' or 'curl --manual' for more information Commented Jun 23, 2018 at 4:48
  • -k is lower case, why did you use httpS ? Commented Jun 23, 2018 at 4:51
  • Sorry, my bad. So I changed it to -k and used http but now I'm The browser (or proxy) sent a request that this server could not understand." Commented Jun 23, 2018 at 4:54
  • found any solution here ? Commented Aug 13, 2018 at 17:46

1 Answer 1

1

My solution to read csv from Flask is:

in Flask:

f = request.files['file']

f will be a handler to the file, you can then use csv_reader.

and to send the file:

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

And that should works. Let me know.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.