用python bottle搭建的http服务post请求405怎么解决啊?

网上有方案说改核心代码

class HTTPResponse(Response, BottleException):
    def __init__(self, body='', status=None, headers=None, **more_headers):
        super(HTTPResponse, self).__init__(body, status, headers, **more_headers)
 
    def apply(self, response):
        response._status_code = self._status_code
        response._status_line = self._status_line
        if self._headers:
            if response._headers:
                response._headers.update(self._headers)
            else:
                response._headers = self._headers
 
        response._cookies = self._cookies
        response.body = self.body

或者

#!/usr/bin/python  
# -*- conding:utf-8 -*-  
  
from bottle import *  
  
#decorator  
def allow_cross_domain(fn):  
    def _enable_cors(*args, **kwargs):  
        #set cross headers  
        response.headers['Access-Control-Allow-Origin'] = '*'  
        response.headers['Access-Control-Allow-Methods'] = 'GET,POST,PUT,OPTIONS'  
        allow_headers = 'Referer, Accept, Origin, User-Agent'  
        response.headers['Access-Control-Allow-Headers'] = allow_headers       
        if bottle.request.method != 'OPTIONS':  
            # actual request; reply with the actual response  
            return fn(*args, **kwargs)      
    return _enable_cors  
     
 
@route('/helloworld/:yourwords', methods=['GET', 'POST'])  
@allow_cross_domain                                              #在此处加上定义的函数  
def hello(yourwords):  
    return 'hello world. ' + yourwords  
  
run(host='0.0.0.0', port=8080)  

我发现都不行,get请求可以的,post请求就405 Method Not Allowed

我的代码

from bottle import route, run, request, response

@route('/color/a')
def colora():
    print(request.forms.get('words'))
    print('xxxx')
    return '{"name":"test"}'

run(host='0.0.0.0', port=50001, debug=True)
阅读 7.2k
1 个回答

from bottle import get, post, request # or route
@post ('/login') # or @route('/login', method='POST')

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题