Search code examples
pythonapiflaskflask-restful

Flask Restful read file


I have a difficulty : I want to access to the content of a file when I posted with a request.

client.py

url = 'http://127.0.0.1:5000/sendimage/test.jpg'
filename = url.split('/')[-1]
print filename
files = {'media': open(filename, 'rb')}
response = requests.post(url, files=files)

server.py

from flask import Flask, request
from flask_restful import Api, Resource, reqparse
import json

app = Flask(__name__)
api = Api(app)

class sendImage(Resource):

    def post(self, pathImg):
        print "Receiving Image ..."
        # Extraction of the sent file
        return "null"

api.add_resource(sendImage, '/sendimage/<pathImg>')

if __name__ == '__main__':
    app.run(port='5000')

The question is: How can server.py read the data in the file sent by the client.py ??


Solution

  • It should be available in request.files['media']

    You can read full documentation on uploading files with good examples in flask docs http://flask.pocoo.org/docs/1.0/patterns/fileuploads/