Search code examples
pythonflaskroutes

Flask: Getting error 404 on one route specifically


I'm trying to use Flask as the backend to make a small desktop app using Electron and I've hit a weird snag.

Essentially, I have a route, "/appendToList/<some_value>" that should be called via a fetch request from the JavaScript code. It just takes the passed value, which should be a string, and then appends the string to a list for later use.

The route looks like this:

routes.py:

list_o_details:list[str]=[]
@app.route("/appendToList/<some_value>", methods=["POST"])
def appendToList(some_value):
    list_o_details.append(some_value)
    return {"value":f"Appended {some_value}"}

Running it through Electron gives two errors in the console:

Two error messages stating: Error 404, failed to load resource and Uncaught SyntaxError: Unexpected token '<', "<!doctype"... is not valid JSONenter image description here
These errors also appear in the terminal, but only after I open the desktop app's console.

If I run the Flask file separately, and try calling the route with curl, it produces the same error 404:

enter image description here

This is weird because my file contains two other routes, both of which work perfectly fine. Below is the entire file:

from flask import Flask
app = Flask(__name__)

list_o_details:list[str]=[]
list_o_sums:list[int]=[]

@app.route("/", methods=["GET","POST"])
def check():
    return {"result":"Server active!"}

@app.route("/add/<num1>/<num2>", methods=["POST"])
def addNumbers(num1,num2):
    num1=int(num1)
    num2=int(num2)
    sum = num1+num2
    list_o_sums.append(sum)
    return {"sum":f"{num1}+{num2}={sum}"}

@app.route("/appendToList/<some_value>", methods=["POST"])
def appendToList(some_value):
    list_o_details.append(some_value)
    return {"value":f"Appended {some_value}"}

print(app.url_map)

if __name__ == "__main__":
    app.run(debug=True)

enter image description here

As you can see, my code includes print(app.url_map) which shows the following:

enter image description here

Though I don't know why it's printed twice, it does show that the URL of "/appedToList/<some_value>" is recognized.

After some testing, I found that if I make a copy of addNumbers() with only one argument like:

@app.route("/adding/<num1>", methods=["POST"])
def addingNumbers(num1):
    num1=int(num1)
    num2=12
    sum = num1+num2
    list_o_sums.append(sum)
    return {"sum":f"{num1}+{num2}={sum}"}

It also results in the same error:

enter image description here

So, thinking that the number of params was somehow the issue, I changed appendToList() to look like:

@app.route('/appendToList/<some_value>/<placeholder>', methods=["POST"])
def appendToList(some_value,placeholder):
    list_o_details.append(some_value)
    return {"value":f"Appended {some_value}"}

But the errors persisted.

I don't know what's wrong. The route exists, meaning that this is likely some syntax error within the route itself, but I cannot figure out what that may be. Does anyone know what's going on and how to fix this?

Thanks in advance


Solution

  • The solution seems to have been to just turn the computer off and on again several times, write the entire program out again by hand in another file, and just copy the name over.

    I still don't know why the error happened, but I'm suspecting that there may have been either something running in the background that was messing things up or some hidden symbol that I had accidentally copied from one of the references I was using that did it.

    Either way, I guess this is solved? Thanks for everyone who responded