用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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
from bottle import get, post, request # or route
@post ('/login') # or @route('/login', method='POST')