我正在尝试从基于django-rest-api的api提供图像。
到目前为止,我有一个非常基本的api视图,可以加载图像及其mimetype并返回它。
@api_view(['GET'])
def get_pic(request, pk=None, format=None):
//get image and mimetype here
return HttpResponse(image, content_type=mimetype)这一直工作得很好,但是我在iOS上的一些新的图像库上测试了它,发现API返回了一个406错误。
我相信这是因为客户端正在发送一个Accept image/*,这个api视图不会排除它。
对api执行HEAD操作将返回以下内容;
GET, OPTIONS
Connection → keep-alive
Content-Type → application/json
Date → Wed, 17 Jun 2015 10:11:07 GMT
Server → gunicorn/19.2.1
Transfer-Encoding → chunked
Vary → Accept, Cookie
Via → 1.1 vegur
X-Frame-Options → SAMEORIGIN如何更改此接口的Content-Type以接受镜像请求?
我尝试使用正确的media_type添加一个自定义解析器,但他的似乎没有帮助。这是正确的方法吗?还是有一种更容易的方法来从django-rest-framework提供图像?
发布于 2015-06-17 23:30:39
您需要创建一个自定义renderer,如下所示:
class JPEGRenderer(renderers.BaseRenderer):
media_type = 'image/jpeg'
format = 'jpg'
charset = None
render_style = 'binary'
def render(self, data, media_type=None, renderer_context=None):
return datahttps://stackoverflow.com/questions/30889003
复制相似问题