[英]Flask-Restful Error: request Content-Type was not 'application/json'."}
[英]How to customize response content type in flask-restful?
我希望能夠返回 application/json 以外的東西,即 kml。
我有以下幾點:
@api.representation('application/vnd.google-earth.kml+xml')
def kml(data):
return Response(data, mimetype='application/vnd.google-earth.kml+xml')
class mykml(restful.Resource):
def get(self):
r = requests.get("http://myurl/kml") # This retrieves a .kml file
response = make_response(r.content)
response.headers['Content-Type'] = "application/vnd.google-earth.kml+xml"
return response
為什么這仍然返回 application/json? 另外,如果我有不同的格式,我可以在沒有裝飾器的情況下動態更改 mykml 類中響應的 Content-Type 嗎?
導入: from flask import Flask, request, Response, session,make_response
如果您需要來自 API 方法的特定響應類型,則必須使用 flask.make_response() 返回“預烘焙”響應對象:
def get(self):
response = flask.make_response(something)
response.headers['content-type'] = 'application/vnd.google-earth.kml'
return response
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.