Skip to content
Advertisement

How do I get the user agent with Flask?

I’m trying to get access to the user agent with Flask, but I either can’t find the documentation on it, or it doesn’t tell me.

Advertisement

Answer

JavaScript

You can also use the request.user_agent object which contains the following attributes which are created based on the useragent string:

  • platform (windows, linux, macos, etc.)
  • browser (chrome, firefox, msie, etc.)
  • version
  • language
  • string (== request.headers.get('User-Agent'))

Note: As of werkzeug 2.0, the parsed data of request.user_agent has been deprecated; if you want to keep getting details you need to use a custom UserAgent implementation and set it as user_agent_class on a custom Request subclass, which is set as request_class on the Flask instance (or a subclass).

Here’s an example implementation that uses ua-parser:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement