I'm trying to upload an audio file from my iOS application to my flask backend. The POST request goes through, but I get an error saying "The browser (or proxy) sent a request that this server could not understand."
I was looking at the flask documentation to do this, but I can't see what they're doing thats different.
@auth.route('/uploadfile', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
print('post request received')
file = request.files['file']
if file and allowed_file(file.filename):
print('file name is valid and saving')
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return jsonify({'success': 1})
else:
print('file failed to save')
return jsonify({'success': 2})
def allowed_file(filename):
print('in allowed_file')
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['AllOWED_EXTENSIONS']
//Config settings
UPLOAD_FOLDER = '/Users/Michael/Desktop/uploads'
ALLOWED_EXTENSIONS = set(['m4a'])
iOS side
func savePressed(){
var stringVersion = recordingURL?.path
let encodedSound = NSFileManager.defaultManager().contentsAtPath(stringVersion!)
let encodedBase64Sound = encodedSound!.base64EncodedStringWithOptions(nil)
let dict = ["file": encodedBase64Sound]
let urlString = "http://127.0.0.1:5000/auth/uploadfile"
var request = NSMutableURLRequest(URL: NSURL(string: urlString)!)
var session = NSURLSession.sharedSession()
request.HTTPMethod = "Post"
var err: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(dict as NSDictionary, options: nil, error: &err)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
//Completion handler
var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in