Search code examples
pythonflaskflask-restful

why is this flask not showing standard message according to my code?


my rest service sits at http://127.0.0.1:5000, but when i launch it, it gives me 404:

Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

why is that? I want my server to show some status msg like 'service ready'.

The actual function that i will use is accessible and works, when i press 'http://127.0.0.1:5000/parser/tengrinews' and hit enter it outputs the msg i coded in the function in my flask app:

[
  "parsing this website :", 
  "tengrinews"
]

the main code:

from flask import Flask
import requests
from datetime import datetime
from flask import jsonify

app = Flask(__name__)

#this is my std method i can't see
@app.route("/http://127.0.0.1:5000/", methods = ['GET'])
def main():
    return jsonify('service is ready')

@app.route("/parser/<string:website>", methods = ['GET'])
def parse(website):    
    return jsonify("parsing this website :", website   )


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

Solution

  • Change this line -

    @app.route("/http://127.0.0.1:5000/", methods = ['GET'])
    to
    @app.route("/", methods = ['GET']).

    Because you have to specify only the extended URL that will be used. The @app.route decorator handles the rest for us

    Note* (Don't do this. For fun only) -
    If you wish to continue to use @app.route("/http://127.0.0.1:5000/", methods = ['GET']) then access the endpoint with the url - http://localhost:5000/http://127.0.0.1:5000/. You will get the response as "service is ready"