Python get http trailers

Viewed 428

How do I get HTTP trailer response headers with python? I am using requests.get and I am unable to get some of the X-Content-Type or X-Status. When I use curl with -i though, I get these responses. Do I need to send a specific header or parameter? I tried sending TE: trailers but it doesnt seem to do anything.

Some sample code:

import requests

url = "some_api_endpoint"
params = {"param1": "val1"}
headers = {"Accept": "application/json", "TE": "trailers"}

response = requests.get(url, params=params, headers=headers, verify=False)
print("Status: {}".format(response.status_code))
print("Content: {}".format(response.content.decode('utf-8')))
print("Headers: {}".format(response.headers))
print("Reason: {}".format(response.reason))

Output:

Status: 200
Content:
Headers: {'Connection': 'close', 'Content-Type': 'application/json', 'Transfer-Encoding': 'chunked'}
Reason: OK

curl command

curl -k -i <url>?<params> -H 'Accept: application/json'

Output:

TP/1.1 200 OK
Connection: close
Content-Type: application/json
Transfer-Encoding: chunked

X-Status: Some Error

I would like to get the Some Error or just even the header X-Status above from python. Basically got the 200 from the server and while streaming some of the response, it hit an error in the middle, causing the error. Unfortunately, I cant get this message easily in python.

1 Answers

From https://www.python.org/dev/peps/pep-0444/#request-trailers-and-chunked-transfer-encoding

Request Trailers and Chunked Transfer Encoding

When using chunked transfer encoding on request content, the RFCs allow there to be request trailers. These are like request headers but come after the final null data chunk. These trailers are only available when the chunked data stream is finite length and when it has all been read in. Neither WSGI nor Web3 currently supports them.

About the requests library there's nothing better:

As stated there Trailers is a very rarely used HTTP functionnality.

It's quite complex to implement well (as you must ensure you do not parse here any header that would alter the size of the body like Content-Length and Transfer-encoding).

So they are really few parsers that implement trailers and very few occurences in the HTTP messages nowadays (but you have one, feel sorry for you :-) ).

Related