12

I'm trying to issue a POST request within a Jinja template in Flask. However, parameters are passed in via GET by default, and this particular method only accepts POST requests.

I tried specifying _method, as below, but it still passes the parameter with GET instead of POST.

<li><a href = "{{ url_for('save_info', _method='POST', filepath=s.name ) }}"><div class="nodes">{{ s.title}} - {{ song.owner }}</div></a></li>

(The error message is the same whether or not I specify _method).

chimeracoder
  • 20,648
  • 21
  • 60
  • 60

3 Answers3

18

All links are GET requests. You can't force a POST.

An alternative would be this:

@app.route('/save_info/<filepath>', methods=['GET', 'POST'])
def save_info(filepath):
  if request.method == 'POST' or filepath:
    ...

You'll have to find a nice way to force your code to ignore that you sent a GET request.

Blender
  • 289,723
  • 53
  • 439
  • 496
  • Hm, is there some other way of doing this without passing the information through the URL, then, the way forms do? This isn't actually a form per-se in the way it's presented to the user, but it's the same idea (a selection presented to the user). – chimeracoder Feb 12 '12 at 07:04
  • You can't send a `POST` request without AJAX or a form, sadly. – Blender Feb 12 '12 at 07:14
2

You could either make a form that contains only a submit button, or send the POST using AJAX or some other client side scripting. As far as I know, you can't make a link send a POST, though.

Jarek
  • 1,320
  • 3
  • 11
  • 19
2

You can add a Middelway which search for a GET argument which overwrite the http method. Look there: http://flask.pocoo.org/snippets/38/

Your new link will look like this:

<li><a href = "{{ url_for('save_info', __METHOD_OVERRIDE__='POST', filepath=s.name ) }}"><div class="nodes">{{ s.title}} - {{ song.owner }}</div></a></li>
Jarus
  • 1,748
  • 2
  • 13
  • 22