Django中图像的二进制数据作为HttpResponse错误

e7arh2l6  于 2023-04-22  发布在  Go
关注(0)|答案(1)|浏览(135)

我尝试将图像的二进制数据作为HttpResponse发送给python request lib生成的post请求。我的视图函数如下:

def view(request):
    
    print("--------------------------------------------")
    print("type of request:", request.method)
    
    if request.method =="POST":
        
        print( "line 55 EPD, content of POST:", request.body)
        print( "META", request.META)
        print( "FILES", request.FILES)
        print( "headers", request.headers)
        print( "all", request)
        print("Content_params",request.content_params)
        print("COOKIES", request.COOKIES)
        print("accepts", request.accepts("application/octet-stream"))
        print("session:", request.session)
               
      
        with open(r"C:\....\main\image.bin", mode='rb') as p:
            Image=p.read()
            print(type(Image))
            
        return HttpResponse( Image, headers={'Content-Type': 'application/octet-stream'} )

我的生成请求的单元测试如下:

def make_request():
    import requests
           
    url = 'http://localhost:8000/view/'
    print("before sending the POST")
    print("sending")
    parameters = ( {
        'Size':0,
        'length':1,
    })
    
    
    postresponse = requests.post(url,json=parameters,stream=True, headers= {'Content-Type':'application/json; charset=utf-8'}, timeout=10)
    print(postresponse.status_code)
    print("Now print Response")
    print("headers:", postresponse.headers)
    print("encoding:", postresponse.encoding)
    print("raw:",postresponse.raw)
    print("content:", postresponse.content)

make_request()

当我的服务器运行时,我收到了成功传输的响应,但在单元测试代码终端,我收到了如下错误:

C:...\requests\models.py", line 899, in content
    self._content = b"".join(self.iter_content(CONTENT_CHUNK_SIZE)) or b""

我收到的输出,直到postresponse.raw,但无法输出postresponse.content.任何建议是高度赞赏。
我尝试使用JsonResponse作为返回,但我得到一条错误消息,指出字节对象不可序列化。

b1zrtrql

b1zrtrql1#

我想我明白你的错误来自哪里了。Django中的HttpResponse对象是设计来处理基于文本的响应的。为了返回像你的图像这样的二进制数据,你应该使用HttpResponse子类StreamingHttpResponse。
可以按如下方式修改视图函数:

from django.http import StreamingHttpResponse

def view(request):
    
    print("--------------------------------------------")
    print("type of request:", request.method)
    
    if request.method == "POST":
        
        print("line 55 EPD, content of POST:", request.body)
        print("META", request.META)
        print("FILES", request.FILES)
        print("headers", request.headers)
        print("all", request)
        print("Content_params",request.content_params)
        print("COOKIES", request.COOKIES)
        print("accepts", request.accepts("application/octet-stream"))
        print("session:", request.session)
               
      
        with open(r"C:\....\main\image.bin", mode='rb') as p:
            Image = p.read()
            print(type(Image))
            
        response = StreamingHttpResponse(Image, content_type='application/octet-stream')
        response['Content-Disposition'] = 'attachment; filename="image.bin"'
        return response

我们将Content-Disposition头设置为'attachment;filename=“image.bin”'来告诉浏览器下载文件而不是内联显示它。但如您所愿。
告诉我对你是否有效

相关问题