-1

I can across a script on github that send a Post request using flask.

I am trying run the script to establish a connection on my local host. But I get the below response

The method is not allowed for the requested URL.

@app.route('/', methods = ['POST'])
def index():
    if 'file' not in request.files:
        return redirect(request.url)
    file = request.files['file']
    # if user does not select file, browser also
    # submit a empty part without filename
    if file.filename == '':
        flash('No selected file')
        return redirect(request.url)
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        warped = transform_file(file)
        return send_from_directory(app.config['UPLOAD_FOLDER'], filename)



if __name__ == '__main__':
    app.run(host='127.0.0.1', threaded=True)
chuky pedro
  • 756
  • 1
  • 8
  • 26
  • Does this answer your question? [Method Not Allowed flask error 405](https://stackoverflow.com/questions/21689364/method-not-allowed-flask-error-405) – Gino Mempin Jan 06 '21 at 12:24
  • how do you connect to this code ? – furas Jan 06 '21 at 14:09
  • if you want to connect from external computer then you have to use `host="0.0.0.0"` instead of `host='127.0.0.1'` because `127.0.0.1` doesn't accept requests from other computers - only from programs running on your computer. – furas Jan 06 '21 at 14:13

2 Answers2

0

you are trying to have only a root i.e "/" in your route.

Can you try with a site, something like below:

@app.route('/', methods=['GET', 'POST'])
CoderRambo
  • 417
  • 3
  • 9
  • that does not seem to work for me. I have never used flask before, I just saw the script and I decided to try it out before replicating same using Django. I get a `302 error` This page isn’t working – chuky pedro Jan 06 '21 at 12:56
  • ok suppose if you are referring to the this https://flask.palletsprojects.com/en/1.1.x/patterns/fileuploads/ then I think you have missed the return part in the end ` return ''' Upload new File

    Upload new File

    ''' ` can you include the return part in the code and see
    – CoderRambo Jan 06 '21 at 13:08
0
  • First of all it's because you do not have a GET method for the route: / address but only POST and that's the reason why Flask displays that error.

  • Definitely you should have a return statment at the end of function with either html code as @CoderRambo wrote in the comment or using render_template function pointing html file inside the templates folder.

Also if your going to use:

@app.route('/', methods=['GET', 'POST'])

you should add an if statment if request.method == 'POST': beforehand that partif 'file' not in request.files: - that would handle the post request and in else block have the return statment that will display the page onto which your going to enter data you'd like to post. As Flask's documention shows https://flask.palletsprojects.com/en/1.1.x/quickstart/: None

Hope I helped.

Cheers