尝试在Python 3.8.3上执行以下代码时,我看到“ 405:不允许使用方法”

时间:2020-08-30 13:03:48

标签: aiohttp python-3.8

https://tutorialedge.net/python/create-rest-api-python-aiohttp/

从aiohttp导入网站 导入json

async def handle(request):
    response_obj = { 'status' : 'success' }
    return web.Response(text=json.dumps(response_obj))


async def new_user(request):
    try:
        print("AT Epoint" )
        ## happy path where name is set
        user = request.query['name']
        ## Process our new user
        print("Creating new user with name: " , user)

        response_obj = { 'status' : 'success' }
        ## return a success json response with status code 200 i.e. 'OK'
        return web.Response(text=json.dumps(response_obj), status=200)
    except Exception as e:
        print("AT EXCEPTION" )
        ## Bad path where name is not set
        response_obj = { 'status' : 'failed', 'reason': str(e) }
        ## return failed with a status code of 500 i.e. 'Server Error'
        return web.Response(text=json.dumps(response_obj), status=500)

1 个答案:

答案 0 :(得分:0)

实现了侦听器功能后,请在aiohttp实例中注册它。

app.router.add_post('/user', new_user)

来源:TutorialEdge