Issue a POST request with url_for in Flask


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', ) }}"><div class="nodes">{{ s.title}} - {{ song.owner }}</div></a></li>

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

8/15/2018 5:52:14 AM

Accepted Answer

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.

2/12/2012 6:23:31 AM

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow