简体   繁体   English

Python Flask:错误“请求的 URL 不允许使用该方法”

[英]Python Flask: Error "The method is not allowed for the requested URL"

I am very new to working with Python Flask and i wanted to try a simple API-example:我对使用 Python Flask 非常陌生,我想尝试一个简单的 API 示例:

from flask import Flask, jsonify, request
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

class HelloWorld(Resource):
    def get(self):
        some_json = request.get_json()
        return {'you sent': some_json}, 201

class Multi(Resource):
    def get(self,num):
        return {'result': num*10}

api.add_resource(HelloWorld, '/')
api.add_resource(Multi,'/multi/<int:num>')

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

and if I type in the terminal如果我在终端输入

-H "Content-Type: application/json" -X POST -d '{"name":"xyz","address":"myaddress"}' http://127.0.0.1:5000/

I get the following message:我收到以下消息:

{
    "message": "The method is not allowed for the requested URL."
}

I hope someone can help me with this...我希望有人能帮我解决这个问题...

Since your are calling the POST HTTP method.由于您正在调用 POST HTTP 方法。 You should rename 'get' function in class HelloWorld to 'post'.您应该将 HelloWorld 类中的“get”函数重命名为“post”。 'HelloWorld' class can also have both 'get' and a 'post' functions if '/' endpoint should serves both. 'HelloWorld' 类也可以同时具有 'get' 和 'post' 函数,如果 '/' 端点应该同时提供两者。

from flask import Flask, jsonify, request
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

class HelloWorld(Resource):
    def post(self):
        some_json = request.get_json()
        return {'you sent': some_json}, 201

class Multi(Resource):
    def get(self,num):
        return {'result': num*10}

api.add_resource(HelloWorld, '/')
api.add_resource(Multi,'/multi/<int:num>')

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python、Flask:方法不允许,请求的方法不允许 URL - Python, Flask: Method Not Allowed, The method is not allowed for the requested URL Python flask flask不允许的方法所请求的URL不允许使用该方法 - Python flask Method Not Allowed The method is not allowed for the requested URL Flask 错误:“方法不允许 请求的 URL 不允许该方法” - Flask Error: “Method Not Allowed The method is not allowed for the requested URL” Flask 错误:“方法不允许 请求的 URL 不允许该方法” - Flask Error: "Method Not Allowed The method is not allowed for the requested URL" Flask 错误:方法不允许 请求的 URL 不允许该方法 - Flask error: Method Not Allowed The method is not allowed for the requested URL Flask 错误:方法不允许 请求的 URL 不允许该方法 - Flask error : Method Not Allowed The method is not allowed for the requested URL Flask - POST - 请求的URL不允许使用该方法 - Flask - POST - The method is not allowed for the requested URL 所请求的URL不允许使用该方法。 在烧瓶 - The method is not allowed for the requested URL. in Flask 如何修复 Flask 中的“请求的 URL 不允许使用该方法” - How to fix "The method is not allowed for the requested URL" in Flask 所请求的URL不允许使用该方法-Flask + HTML - The method is not allowed for the requested URL - Flask + HTML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM