Iterating streaming_content attribute on django FileResponse

I am working on an api with django rest framework and I am writing some unit tests to validate critical operations. I am trying to read the content of a file after a request request

downloaded_file = self.client.get(textfile)

      

The problem is that the returned object is of type: django.http.response.FileResponse, which inherits from StreamingHttpResponse .

I'm trying to iterate over the streaming_content attribute, which is supposedly an iterator, but I can't iterate, not a method next()

.

I have checked this object and I am getting the map object. Any ideas on how to get content from this request?

Edit:

Solution to the problem

The returned object is a map, the map takes a function and an iterable:

https://docs.python.org/3.4/library/functions.html#map

I needed to make a map in a list, access the first element of the list, and convert from bytes to string. Not very elegant, but it works.

list(response.streaming_content)[0].decode("utf-8")

      

+3


source to share


1 answer


This is how you fetch content from the map streaming_content

:

content = downloaded_file.getvalue()

      



Looking at the code for the method getvalue()

, we can see that it simply iterates over the content of the response:

class StreamingHttpResponse(HttpResponseBase):
    ...
    def getvalue(self):
        return b''.join(self.streaming_content)

      

+1


source







All Articles