Binary data of Image as HttpResponse in Django

I am trying to send the binary data of the image as a HttpResponse for the post request generated by python request lib. My view function is as follows :

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'} 

and my unit test for generating request is as follows:

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()

When my server is running, I receive the successful transfer of the response, but in the unit test code terminal, I receive the error as below:

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

I receive the output until postresponse.raw but unable to output postresponse.content. Any suggestion is highly appreciated.

Normally you don’t this type of operations (serving static files) on django, neither with a POST request.
Normally this requests uses the GET method, and on production this operation is handled by your server not your application. In development django can serve static files, but won’t do so in production.

On django normally you set this up using the staticfiles app:You can take a look into the staticfiles app serve view, documented here.

If your use case is not normal, it would be helpful to describe what you’re trying to accomplish.