Skip to content
Advertisement

How to access formData in flask sent using websockets?Flask-SocketIO

How to access form data sent to Flask using web sockets? I receive invalid frame header in google chrome developer tools->console.

Extract from my javascript code:

var form_data = new FormData($('#my_form')[0]); 

socket.emit('handle_form',{data:form_data});

How would I access, say ‘title’ field in my_form from flask ?

request.form throws the same error “Invalid frame header”

One more question, is it good to use web sockets for form submission and as an entire replacement for ajax ?

Advertisement

Answer

Instead of sending a FormData object, which is a client-side only construct, you should build a plain dictionary and send that, as all the data that is transferred back and forth in Socket.IO is serialized to JSON.

Then on the server, you will have a dict that is sent as an argument to your socket callback function. See this example for ideas on how to send form data to the server.

Advertisement