[Fixed]-Django Rest Framework Testing response headers

22👍

There are several options:

>>> response.has_header('Location')
True

>>> response.get('Location')  # None if key not in headers
My location

>>> response['Location']  # KeyError if key doesn't exist
My location

>>> response._headers  # headers as dict
{'allow': ('Allow', 'GET, POST, HEAD, OPTIONS'), 'Location': ...}

>>> response.serialize_headers()  # headers as bytestring (in Python 3)
b'Allow: GET, POST, HEAD, OPTIONS\r\nLocation: ...'
👤yofee

4👍

In case, someone encounters the same issue. The code to print out or return the Location headers is:

# url, just set your endpoint here
# data, just set the data that you will request here
response = self.client.post(url, data)
response["Location"]

Source: https://github.com/tomchristie/django-rest-framework/issues/4301#issuecomment-235224120

Leave a comment