Ayr*_*yrx 17 python flask python-2.7 flask-restful
我已根据Flask-RESTful文档定义了自定义响应格式,如下所示.
app = Flask(__name__)
api = restful.Api(app)
@api.representation('application/octet-stream')
def binary(data, code, headers=None):
resp = api.make_response(data, code)
resp.headers.extend(headers or {})
return resp
api.add_resource(Foo, '/foo')
Run Code Online (Sandbox Code Playgroud)
我有以下资源类.
class Foo(restful.Resource):
def get(self):
return something
def put(self, fname):
return something
Run Code Online (Sandbox Code Playgroud)
我希望get()函数返回application/octet-stream类型和put()函数以返回默认值application/json.
我该怎么做呢?关于这一点,文档不是很清楚.
Mar*_*ers 18
使用什么表示由请求(Accept标题mime类型)确定.
application/octet-stream将使用您的binary功能回复请求.
如果您需要API方法中的特定响应类型,则必须使用flask.make_response()返回"预烘焙"响应对象:
def get(self):
response = flask.make_response(something)
response.headers['content-type'] = 'application/octet-stream'
return response
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12364 次 |
| 最近记录: |