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.