How to read a csv django http response

fildred13

In a view, I create a Django HttpResponse object composed entirely of a csv using a simply csv writer:

response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="foobar.csv"'

writer = csv.writer(response)

    table_headers = ['Foo', 'Bar']
    writer.writerow(table_headers)

    bunch_of_rows = [['foo', 'bar'], ['foo2', 'bar2']]
    for row in bunch_of_rows:
        writer.writerow(row)

return response

In a unit test, I want to test some aspects of this csv, so I need to read it. I'm trying to do so like so:

response = views.myview(args)

reader = csv.reader(response.content)

headers = next(reader)
row_count = 1 + sum(1 for row in reader)

self.assertEqual(row_count, 3) # header + 1 row for each attempt
self.assertIn('Foo', headers)

But the test fails with the following on the headers = next(reader) line:

nose.proxy.Error: iterator should return strings, not int (did you open the file in text mode?)

I see in the HttpResponse source that response.content is spitting the string back out as a byte-string, but I'm not sure the correct way to deal with that to let csv.reader read the file correctly. I thought I would be able to just replace response.content with response (since you write to the object itself, not it's content), but that just resulted in a slight variation in the error:

_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

Which seems closer but obviously still wrong. Reading the csv docs, I assume I am failing to open the file correctly. How do I "open" this file-like object so that csv.reader can parse it?

miyamoto

response.content provides bytes. You need to decode this to a string:

foo = response.content.decode('utf-8')

Then pass this string to the csv reader using io.StringIO:

import io
reader = csv.reader(io.StringIO(foo))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to read a csv django http response

From Dev

How to return HTTP 400 response in Django?

From Dev

How to change timezone in http response (django server)?

From Dev

how Django reads http request and sends http response

From Dev

How to read JSON error response from $http if responseType is arraybuffer

From Dev

How to read the HTTP response body in catch exception strategy

From Dev

How to parse http/https response and export it to a csv file

From Dev

how to upload and read csv file in django using csv.DictReader?

From Dev

How to set far expires date on http response in django?

From Dev

How do I add a Python list to a Django HTTP response

From Dev

How to read a json response from a akka-http response entity using json4s

From Dev

How to read JSON response?

From Dev

How to format the HTTP response

From Dev

How to format the HTTP response

From Dev

AngularJS - Cannot read response headers from $http

From Dev

unable to read response in http client using netty

From Dev

Is it necessary to read data before close http response?

From Dev

IOException when trying to read http connection response

From Dev

Is it necessary to read data before close http response?

From Dev

How to read RAW JSON in Angular JS from HTTP get method, when response type is arraybuffer?

From Dev

How do I read a streaming response body using Golang's net/http package?

From Dev

How to read RAW JSON in Angular JS from HTTP get method, when response type is arraybuffer?

From Dev

How to read protobuf object in angular4 received in response of an HTTP request

From Dev

How to read content from http 200 response from server built using python flask?

From Dev

Jquery - Read HTTP response from a promise/deferred Ajax response

From Dev

How to read the response from a NewSingleHostReverseProxy

From Dev

How to read response headers in angularjs?

From Dev

How to read AJAX response variable?

From Dev

How to read response headers with $resource?

Related Related

  1. 1

    How to read a csv django http response

  2. 2

    How to return HTTP 400 response in Django?

  3. 3

    How to change timezone in http response (django server)?

  4. 4

    how Django reads http request and sends http response

  5. 5

    How to read JSON error response from $http if responseType is arraybuffer

  6. 6

    How to read the HTTP response body in catch exception strategy

  7. 7

    How to parse http/https response and export it to a csv file

  8. 8

    how to upload and read csv file in django using csv.DictReader?

  9. 9

    How to set far expires date on http response in django?

  10. 10

    How do I add a Python list to a Django HTTP response

  11. 11

    How to read a json response from a akka-http response entity using json4s

  12. 12

    How to read JSON response?

  13. 13

    How to format the HTTP response

  14. 14

    How to format the HTTP response

  15. 15

    AngularJS - Cannot read response headers from $http

  16. 16

    unable to read response in http client using netty

  17. 17

    Is it necessary to read data before close http response?

  18. 18

    IOException when trying to read http connection response

  19. 19

    Is it necessary to read data before close http response?

  20. 20

    How to read RAW JSON in Angular JS from HTTP get method, when response type is arraybuffer?

  21. 21

    How do I read a streaming response body using Golang's net/http package?

  22. 22

    How to read RAW JSON in Angular JS from HTTP get method, when response type is arraybuffer?

  23. 23

    How to read protobuf object in angular4 received in response of an HTTP request

  24. 24

    How to read content from http 200 response from server built using python flask?

  25. 25

    Jquery - Read HTTP response from a promise/deferred Ajax response

  26. 26

    How to read the response from a NewSingleHostReverseProxy

  27. 27

    How to read response headers in angularjs?

  28. 28

    How to read AJAX response variable?

  29. 29

    How to read response headers with $resource?

HotTag

Archive