Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Flask not receiving my POST Blob?

My problem is that I'm sending a Blob to the server with FormData, but the server isn't getting it. Code looks like the following:

Flask server code:

@app.route('/save-record', methods=['POST'])
def save_record():
    app.logger.debug(request.form.keys()) #Shows only the title key, not the file key

Client JS Code:

var save = function() {
    var form = new FormData();
    form.append('file', record.blob, record.filename);
    form.append('title', searchedObj.title);
    //Chrome inspector shows that the post data includes a file and a title.                                                                                                                                           
    $.ajax({
      type: 'POST',
      url: '/save-record',
      data: form,
      cache: false,
      processData: false,
      contentType: false
    }).done(function(data) {
      console.log(data);
    });
like image 717
user592419 Avatar asked Aug 17 '14 00:08

user592419


People also ask

How do I get POST JSON data in Flask?

To get the posted JSON data, we just need to call the get_json method on the request object, which parses the incoming JSON request data and returns it [2] as a Python dictionary. You can check the full code bellow, which already includes the calling of the run method on the app object, to start the server.

How do you handle a post request in Flask?

In order to demonstrate the use of POST method in URL routing, first let us create an HTML form and use the POST method to send form data to a URL. Now enter the following script in Python shell. After the development server starts running, open login. html in the browser, enter name in the text field and click Submit.

How do you process incoming request data in Flask?

JSON Data. For this request, we will need to use Postman to send the requests. In Postman, add the URL http://127.0.0.1:5000/json-example and change the type to POST. On the body tab, change to raw and select JSON from the drop-down and copy-paste the JSON data above into the text input.

What is request form Flask?

In the client-server architecture, the request object contains all the data that is sent from the client to the server. As we have already discussed in the tutorial, we can retrieve the data at the server side using the HTTP methods.


1 Answers

Check request.files for the file.

@app.route('/save-record', methods=['POST'])
def save_record():
    app.logger.debug(request.files['file'].filename) 
like image 126
Musa Avatar answered Sep 28 '22 00:09

Musa