"Lost data" while processing data in WTForm

129 views Asked by At

I send a HTTP request from a client. Then on the server I try to validate a WTForm Form.

from webob.multidict import MultiDict

from wtforms import Form, TextField, PasswordField, validators

class LoginForm(Form):
    email = TextField('Email', [validators.Required(), validators.Email()])
    password = PasswordField('Password', [validators.Required()])

The following commands are executed inside a RequestHandler:

self.request.body
>>> '{"username":"[email protected]", "password":"pass"}'

json.loads(self.request.body)
>>>  {"username":"[email protected]", "password":"pass"}

type(json.loads(self.request.body))

>>> type 'dict'

MultiDict(json.loads(self.request.body))

>>> MultiDict[(u'username', u'[email protected]'), (u'password':'******')])

LoginForm(MultiDict(json.loads(self.request.body))).data

>>> {'password': 'pass', 'email': u''}

From the last command I'm expecting to see {"username":"[email protected]", "password":"pass"} which I can later validate. However, somehow the actual data is 'lost'. Any ideas?

1

There are 1 answers

0
X-Istence On

Your login form has the fields named email and password yet you are passing it the fields username and password.

Change one or the other to match, and you should be good to go.