Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 不允许使用Python Flask POST请求405方法的Web API_Python 3.x_Api_Web_Request - Fatal编程技术网

Python 3.x 不允许使用Python Flask POST请求405方法的Web API

Python 3.x 不允许使用Python Flask POST请求405方法的Web API,python-3.x,api,web,request,Python 3.x,Api,Web,Request,我正在为我编写的python程序制作一个Web API,我正在复制一个教程 这是API代码 #!flask/bin/python from flask import Flask from flask import make_response from flask import request import requests import json app = Flask(__name__) @app.route('/') def index(): return "Hello

我正在为我编写的python程序制作一个Web API,我正在复制一个教程

这是API代码

#!flask/bin/python
from flask import Flask
from flask import make_response
from flask import request
import requests
import json

app = Flask(__name__)

@app.route('/')
def index():
    return "Hello, World!"

if __name__ == '__main__':
    app.run(debug=True)
    
@app.errorhandler(404)
def not_found(error):
    return make_response(jsonify({'error': 'Not found'}), 404)
  
@app.route('/5492946838458/index.html', methods=['POST'])
def create_task():
    if not request.json or not 'key' in request.json or not 'name' in request.json or not 'text' in request.json or not 'pack' in request.json:
        abort(400)
    if 'title' in request.json and type(request.json['title']) != unicode:
        abort(400)
    if 'description' in request.json and type(request.json['description']) is not unicode:
        abort(400)
    task = {
      'key': request.json['key'],
      'name': request.json['name'],
      'text': request.json['text'],
      'pack': request.json['pack']
    }
    return (200)
这是我要发送到的URL

https://my.websites.url.here/5492946838458/
还有我发送的json数据

{
"key": "key",
"name": "name",
"text": "text",
"pack": "pack"
}
我得到的头球,我得到的

date: Fri, 04 Sep 2020 17:48:30 GMT
content-length: 0
vary: Origin
accept-ranges: bytes
allow: GET, HEAD, OPTIONS

为什么会发生这种情况?我如何解决这两个我能看到的问题

这行不应该在代码的中间浮动。它应该在最后:

if __name__ == '__main__':
    app.run(debug=True)
在当前位置下,如果您使用
python app.py
执行应用程序,应用程序将在此时运行。之前的路由(
索引
)将可用,但之后声明的路由(
创建_任务
)将不可用(直到您杀死服务器-添加后一个路由时,
python
进程停止之前)

如果使用
flask run
执行,则不会出现此问题,因为
if
子句为False


@app.route('/5492946838458/index.html',methods=['POST'])

对于这一个,您可能需要:

@app.route('/5492946838458/',methods=['POST'])

这声明了该路由的URL

现在请求
https://my.websites.url.here/5492946838458/
应返回成功的响应。对
/5492946838458
的请求将返回308重定向到带有尾随斜杠的重定向

我不知道你以前为什么得到
405
。也许在代码中的某个地方有另一条接受请求的路径,但不是方法