0

I have a csv file, which I need to post on my server, convert it to json and send it back. With JSON, you can simply do request.json(Force=True), however I am not sure how to make flask to read my csv.

So far I have this:

@application.route('/postcsv', methods=['POST'])
def csv_view():
    content = request.files(force=True)

    stream = io.StringIO(content.stream.read().decode("UTF-8"), newline = None)
    csv_input = csv.reader(stream)
    print(csv_input)
    return csv_input


if __name__ == '__main__':
application.run(debug=True, host='0.0.0.0')

The error I am getting is TypeError: 'ImmutableMultiDict' object is not callable. I think my approach overall is wrong but I am not sure

yorodm
  • 4,359
  • 24
  • 32
  • I assume the error come from the following line: `content = request.files(force=True)`. Instead, you should use `content = request.files['parameter_name']`. There is more information [here](https://stackoverflow.com/questions/20015550/read-file-data-without-saving-it-in-flask) – Vincent Feb 20 '19 at 18:08
  • 1
    Share the stacktrace please – balderman Feb 20 '19 at 18:10
  • This also mixes the `request.json` attribute with the `request.get_json()` method which does permit an optional `force=True` keyword parameter for accepting form data which doesn't have the expected `application/json` content type. I'm at loss as to what the OP might perceive to be the expected content type for `files`. – tripleee Feb 20 '19 at 21:20

1 Answers1

1

You got this error because request.files is not a function and thus can't be called.

Instead, you should use request.files[<KEY>]. See: Not able to parse a .csv file uploaded using Flask

gnetmil
  • 86
  • 4