0

I wish to show my data in a webpage by using flask. (Trying to learn it)

from flask import Flask, jsonify, make_response
from flask_cors import CORS

api = Flask(__name__)
CORS(api)
api.config['JSON_AS_ASCII'] = False
api.config["JSON_SORT_KEYS"] = False

@api.route('/token',methods=["POST"])
def get_token(self):
    data = {
            "type": "testing",
            }
    response1 = make_response(jsonify(data))
    return response1

if __name__ == "__main__":
    api.run(port=11111)

current output when try http://127.0.0.1:11111/ on google chrome:

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

I also tried with /token:

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

4 Answers 4

1

you need to go to http://127.0.0.1:11111/token, if you want to go to http://127.0.0.1:11111/ you need to define a function with route @api.route('/',methods=["POST"])

Also a browser makes GET requests via URL unless explicitly defined, change it to get via @api.route('/',methods=["GET"])

Sign up to request clarification or add additional context in comments.

5 Comments

Can I know when to use the POST method?
for that you may refer to this post stackoverflow.com/questions/504947/…
Do you know why I should include the api.config['JSON_AS_ASCII'] = False api.config["JSON_SORT_KEYS"] = False?
using JSON_AS_ASCII allows you to view letters like ''é'' you dont need to use it, you may choose to
also api.config["JSON_SORT_KEYS"] = False is used to automatically stop python from sorting keys since JSONIFY automatically sorts keys, sometimes thats undesirable so you may choose to set it as False, once again you dont need to use it, you may chose to if the qpp requires it
0

Your route is /token, so you need to go to http://127.0.0.1:11111/token.

1 Comment

I tried but get an error Method Not Allowed The method is not allowed for the requested URL.
0

POST requests cannot be directly viewed in browser. Try some rest API client like Postman to test your POST request.

Alternatively, if you want to test if the API just works, change the POST method to GET. Then if you visit http://127.0.0.1:11111/token, you can see the response. Also you don't need 'self' argument to your method.

Comments

0

You restrict your app.route to only POST. If you want to enter your page from url you have to specify GET as well.

Read about http requests

from flask import Flask, jsonify, make_response
from flask_cors import CORS

api = Flask(__name__)
CORS(api)
api.config['JSON_AS_ASCII'] = False
api.config["JSON_SORT_KEYS"] = False

@api.route('/token',methods=["GET", "POST"])
def get_token(self):
    data = {
        "type": "testing",
        }
    response1 = make_response(jsonify(data))
    return response1

if __name__ == "__main__":
    api.run(port=11111)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.