suren
suren

Reputation: 8766

Get `request` object from python (server) script in flask

I am trying to get the request object itself in Python; to strip out any attribute I need from it. For now, the headers, through flask, but I can't.

All the docs/questions/issues I find is about how to get the attributes from the response, but no word about how to get it from the request.

The context:

I am playing with Istio, and Istio appends some headers to the request, that I need to collect and forward to the next request in order for Istio to keep track of the metrics.

In Python, I can do it as follows:

...
    def do_GET(self):
        print(self.headers)
...

Which dumps the headers:

host: debian.istio-server.svc.cluster.local
user-agent: curl/7.59.0
accept: */*
x-forwarded-proto: http
x-request-id: 398756b5-87aa-93e3-8c02-ccd8e9027db3
x-envoy-decorator-operation: debian.istio-server.svc.cluster.local:80/*
x-istio-attributes: CkMKGGRlc3RpbmF0aW9uLnNlcnZpY2UuaG9zdBInEiVkZWJpYW4uaXN0aW8tc2VydmVyLnN2Yy5jbHVzdGVyLmxvY2FsCkEKF2Rlc3RpbmF0aW9uLnNlcnZpY2UudWlkEiYSJGlzdGlvOi8vaXN0aW8tc2VydmVyL3NlcnZpY2VzL2RlYmlhbgokChhkZXN0aW5hdGlvbi5zZXJ2aWNlLm5hbWUSCBIGZGViaWFuCi8KHWRlc3RpbmF0aW9uLnNlcnZpY2UubmFtZXNwYWNlEg4SDGlzdGlvLXNlcnZlcgpBCgpzb3VyY2UudWlkEjMSMWt1YmVybmV0ZXM6Ly9jdXJsZXItNzU1Y2M3Y2ZmZi1sYnJwNi5pc3Rpby1jbGllbnQ=
x-b3-traceid: eec6020b96fd3bd5fa406dd115edf516
x-b3-spanid: fa406dd115edf516
x-b3-sampled: 1
content-length: 0

I am wondering if there is an equivalent in flask. Or any other way of getting the headers from the request, would be fine.

Upvotes: 0

Views: 63

Answers (1)

Maaz
Maaz

Reputation: 2445

You should be able to import request from flask and read correctly the headers

Something like this:

from flask import request
...
    def do_GET():
        print(request.headers)
...

Upvotes: 1

Related Questions