0

I am new to building API. I am building a very Simple API: When executed, The HTML page displaying API will ask to enter name: the Api will just return: "Hello {name}". I am getting 405 Error. Here is my code for App.py and HTML page:

from app import app
from flask import render_template
from flask import request

@app.route('/')
def home():
   return "hello no  world!"

@app.route('/template',methods=['POST','GET'])
def template():
    output = [request.form.values()]
    output = output[0]
    return render_template('home.html',prediction_text='Hello {}'.format(output))

and my HTML code for home.html:

!doctype html>

<html lang="en-us">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>Welcome home</title>
  </head>

  <body>
    <div class="login">
        <h1>Enter your name</h1>

     <!-- Main Input For Receiving Query-->
    <form action="{{ url_for('template')}}"method="post">
        <input type="text" name="name" placeholder="Name" required="required" />

        <button type="submit" class="btn btn-primary btn-block btn-large">Enter</button>
    </form>

   <br>
   <br>
   {{ prediction_text }}

 </div>
  </body>
</html>

I have looked at several other StackOverflow forums with just this issue. It seems like there is something wrong with "GET" or "POST" method but I can not seem to figure out what? Maybe one of you could see something I did not. I am running this API Inside a Docker so "app = Flask(name)" are stored elsewhere , If that is relevant.

Method Not Allowed - The method is not allowed for the requested URL

Flask - POST - The method is not allowed for the requested URL

https://www.reddit.com/r/flask/comments/a04aew/ask_flask_help_me_to_find_the_error_in_my_code/

  • The Hyper Text Transfer Protocol (HTTP) 405 Method, Not Allowed response status code indicates that the request method is known by the server but is not supported by the target resource. –  May 27 '21 at 20:28
  • So what method you use, POST? What exact request is? How do you run it, through Postman, curl? – Alveona May 27 '21 at 21:34
  • @Alveona Since I run the app inside the Docker, I have separate files I use to run the program using Ubuntu Terminal. As for method, The video I used to build my program uses uses "POST" method exclusively, But I read somewhere if our output is to be displayed on HTML, It needs to have both "GET" and "POST". Everything works fine but when I enter a name and press enter, 405 shows up. – Faraz Ali Khan May 27 '21 at 22:30
  • Update: In my HTML page: I changed my :
    from "post" to "get" and I do get an output now. However its like this: "Hello " and not the name that I entered when I opened the URL. Any ideas?
    – Faraz Ali Khan May 27 '21 at 22:37
  • 1
    You need something like `request.form['name']` to get the value of the textbox, since you've called it 'name'. And your route /template accepts POST requests, that's why this one works with the form but the route / doesn't. – Kate May 28 '21 at 19:23

1 Answers1

0

This question is now resolved. I changed two things:

From HTML: Changing:

<form action="{{ url_for('template')}}"method="post">

to

 <form action="{{ url_for('template')}}"method="get">

And Changing from Flask API:

output = [request.form.values()]
output = output[0]
return render_template('home.html',prediction_text='Hello {}'.format(output))

to

output = request.args.values()
return render_template('home.html',prediction_text="Hello {}?".format(list(output)[:]))