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.

Anonymous
  • 477
  • 3
  • 12

4 Answers4

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"])

Imtinan Azhar
  • 1,725
  • 10
  • 26
  • Can I know when to use the POST method? – Anonymous Aug 07 '19 at 07:15
  • for that you may refer to this post https://stackoverflow.com/questions/504947/when-should-i-use-get-or-post-method-whats-the-difference-between-them/504993 – Imtinan Azhar Aug 07 '19 at 07:17
  • Do you know why I should include the api.config['JSON_AS_ASCII'] = False api.config["JSON_SORT_KEYS"] = False? – Anonymous Aug 07 '19 at 07:17
  • using JSON_AS_ASCII allows you to view letters like ''é'' you dont need to use it, you may choose to – Imtinan Azhar Aug 07 '19 at 07:18
  • 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 – Imtinan Azhar Aug 07 '19 at 07:20
0

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

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
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.

Kavitha Madhavaraj
  • 562
  • 1
  • 6
  • 23
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)
Wojciech
  • 34
  • 6