Flask-RESTful - Return custom Response format

前端 未结 3 386
南旧
南旧 2021-02-01 06:23

I have defined a custom Response format as per the Flask-RESTful documentation as follow.

app = Flask(__name__)
api = restful.Api(app)

@api.representation(\'app         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-02-01 06:48

    In addition to @Martijin Pieters' answer here - https://stackoverflow.com/a/20246014/1869562. Where you return a raw response object, Flask-Restful also allows you to set status code and headers in your return values directly.

    So in your case, this should also work

    class Foo(restful.Resource):
    
        def get(self):
            return something, 201, {'content-type': 'application/octet-stream'}
    

    The default mediatype for Flask-REstful is 'application/json', so put should work as is.

提交回复
热议问题