5

I want to set requests 'user-agent' header globally. But I can't find any way to do it. Can anyone help me?

aadibajpai
  • 356
  • 3
  • 16
benbusi
  • 117
  • 1
  • 8

2 Answers2

6

You can set your global headers in requests.Session() instead:

s = requests.Session()
s.headers['User-Agent'] = 'My Browser'
s.get('http://...') # requests sent from this session will all have this User-Agent header

See requests.Session's documentation for more details.

Or if you want to apply the header globally to requests not made from a session, you can use the following wrapper instead:

import requests
import inspect

def override_headers(self, func, global_headers):
    def wrapper(*args, **kwargs):
        bound = sig.bind(*args, **kwargs)
        bound.apply_defaults()
        bound.arguments.setdefault('headers', {}).update(global_headers)
        return func(*bound.args, **bound.kwargs)

    sig = inspect.signature(func)
    return wrapper

requests.request = override_headers(requests.request, {'User-Agent': 'My Browser'})

so that all requests, including GET, POST, etc., will be sent with the specified headers.

blhsing
  • 91,368
  • 6
  • 71
  • 106
  • I think that there may be a way to set header,and whenever I used request.get,I don't need to set headers – benbusi Sep 25 '18 at 12:43
  • If you mean to set a header globally, then why do you not want to set the header when using `requests.get`? – blhsing Sep 25 '18 at 12:45
  • I have many requests to different urls,I want to set the header globally – benbusi Sep 25 '18 at 12:59
  • Then my answer will work. Simply make all your requests using the same session that has the header set, like I showed above. – blhsing Sep 25 '18 at 13:00
  • I understand your way to set header,But now I have finished coding,and I have hundreds of requests.get in my code,so I want to a simple way to golbally setting – benbusi Sep 25 '18 at 13:10
  • I see. I've updated my answer with a solution to apply the headers globally to requests that are not made in a session. – blhsing Sep 25 '18 at 13:24
  • @blhsing I think this code may have a bug: why the `if headers:` conditional, doesn't that means the `global_headers` will only get injected if there were some other headers set by user? I would expect the `global_headers` to get injected unconditionally. – wim May 19 '20 at 04:43
  • For those who only wanted to globally override the default user agent, then a less intrusive hack would be to monkeypatch [`requests.utils.default_user_agent()`](https://github.com/psf/requests/blob/9ed5db8ed28e816b597dafd328b342ec95466afa/requests/utils.py#L798-L804). – wim May 20 '20 at 05:49
5

You can also monkey-patch default_user_agent, like this:

from requests import utils
DEFAULT_USER_AGENT = 'My Agent'
utils.default_user_agent = lambda: DEFAULT_USER_AGENT

This method is useful when the requests are made from an external package, and you don't want to modify the package's source code.

Dylan Tack
  • 725
  • 6
  • 9