-1

I'm working to modify a cookiecutter Flask app.

I have a form built into the public/flat template that looks like:

      <form class="form-inline" id="registerForm"  method="POST" action="/get_email/" role="form">
          <div class="form-group">
            <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter your email address">
          </div>
          <button type="submit" class="btn btn-warning btn-lg">submitMe!</button>
        </form>

My public blueprint contains:

blueprint = Blueprint('public', __name__, static_folder="../static")

@login_manager.user_loader
def load_user(id):
    return User.get_by_id(int(id))

@blueprint.route("/", methods=["GET"])
def flat():
    return render_template('public/flat.html')

@blueprint.route("/get_email/", methods=['GET', 'POST'])
def get_email():
    email = request.get_data()
    return email

When I tested the email signup form (html above) by submitting only the email, In my debugger I get :

email = ''

In the firefox net panel I see:

POST /get_email/

400 BAD REQUEST

127.0.0.1:5000

192 B

127.0.0.1:5000


31ms
HeadersPostResponseHTMLCacheCookies
view source
Content-Length  
192
Content-Type    
text/html
Date    
Thu, 04 Feb 2016 17:58:42 GMT
Server  
Werkzeug/0.10.4 Python/2.7.5
Set-Cookie  
session=eyJfaWQiOnsiIGIiOiJPVEprT1RsaU5ESXpNamMzTmpjMk5ESmpORGs1WmpGaFlXRTRObVpsWWpJPSJ9fQ.CZUi0g.mHrFfzFfFsX9PIjQmeN-3GQ1c2Y
; HttpOnly; Path=/
view source
Accept  
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding 
gzip, deflate
Accept-Language 
en-US,en;q=0.5
Connection  
keep-alive
Cookie  
csrftoken=kvecASUkun0BdgnLvf87MsW2hiARhVhr; session=eyJfaWQiOnsiIGIiOiJPVEprT1RsaU5ESXpNamMzTmpjMk5ESmpORGs1WmpGaFlXRTRObVpsWWpJPSJ9fQ
.CZUizg.9ZEEVV-XujIY8RQdK1Wk9cwC90M
Host    
127.0.0.1:5000
Referer 
http://127.0.0.1:5000/
User-Agent  
Mozilla/5.0 (Windows NT 6.1; rv:43.0) Gecko/20100101 Firefox/43.0
Content-Length  
0
Content-Type    
application/x-www-form-urlencoded

Why is the post not working?

user1592380
  • 34,265
  • 92
  • 284
  • 515

1 Answers1

2

Forms are submitted using the format key=value. In order for a form to contain a field it must have a name attribute to use as the key.

<input name="email" type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter your email address">

You can update your get_email endpoint to use the value directly.

def get_email():
    email = request.form['email']
dirn
  • 19,454
  • 5
  • 69
  • 74
  • Thanks for looking at it, I made the switch and am now seeing: 400 The browser (or proxy) sent a request that this server could not understand. – user1592380 Feb 04 '16 at 17:25
  • That means there's a `KeyError` (running in debug mode would show that). Either you didn't name the input `email` or you're using other form fields not shown here. – dirn Feb 04 '16 at 17:31
  • this is the entire form as far as i understand, I'm using http://blacktie.co/2013/12/flatty-app-landing-page/ as a template landing page. I've added some details from the browser above. Interestingly it says content length 0. – user1592380 Feb 04 '16 at 18:05
  • That says to me you didn't update your `input` tag to include the name. – dirn Feb 04 '16 at 18:08
  • You were right, Thanks for the explanation! – user1592380 Feb 04 '16 at 19:11