Skip to content

Commit 3b6c0fa

Browse files
committed
added get request header method
1 parent a9d3ed6 commit 3b6c0fa

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

django_petra/helpers.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,28 @@ def get_all_request_data(request):
5858
return combined_data
5959
except Exception as e:
6060
raise ParseError(f"Error extracting all request data: {str(e)}")
61+
62+
def get_request_headers(request):
63+
"""
64+
Gets all headers from the request object.
65+
66+
:param request: Petra request object
67+
:return: Dictionary containing all request headers
68+
"""
69+
try:
70+
# META keys starting with 'HTTP_' contain the HTTP headers
71+
headers = {
72+
key[5:].lower().replace('_', '-'): value
73+
for key, value in request.META.items()
74+
if key.startswith('HTTP_')
75+
}
76+
77+
# Special handling for Content-Type and Content-Length as they don't have HTTP_ prefix
78+
if 'CONTENT_TYPE' in request.META:
79+
headers['content-type'] = request.META['CONTENT_TYPE']
80+
if 'CONTENT_LENGTH' in request.META:
81+
headers['content-length'] = request.META['CONTENT_LENGTH']
82+
83+
return headers
84+
except Exception as e:
85+
raise ParseError(f"Error extracting request headers: {str(e)}")

0 commit comments

Comments
 (0)