User-Agent Parser

Parse and analyze User-Agent strings to identify browser, OS, and device information.

About User-Agent Strings

A User-Agent (UA) string is an HTTP request header that browsers send with every request, identifying the client software: browser name, version, rendering engine, and operating system. Servers use this for analytics, content negotiation, feature detection, and logging. UA strings have a notoriously complex history β€” virtually every browser includes "Mozilla/5.0" for compatibility with early Netscape-aware servers, even though it meaninglessly identifies nothing.

Chrome includes "Safari" in its UA; Safari includes "like Gecko". This compatibility theater makes manual parsing brittle, which is why dedicated parsing libraries are necessary for reliable results.

User-Agent String Components

  • Mozilla/5.0 β€” Legacy compatibility token present in all browsers. Cannot be used to identify any specific browser.
  • Platform / OS β€” In parentheses: (Windows NT 10.0; Win64; x64) or (iPhone; CPU iPhone OS 17_0 like Mac OS X)
  • Rendering engine β€” AppleWebKit/537.36 (Chromium-based), Gecko/20100101 (Firefox)
  • Browser token β€” The actual browser at the end: Chrome/120.0.0.0, Firefox/121.0, Safari/604.1
  • Mobile indicator β€” Mobile browsers include Mobile in the UA string
  • Bot identifier β€” Web crawlers identify themselves: Googlebot/2.1, bingbot/2.0

UA Detection Best Practices

Server-side UA-based browser detection for feature decisions is considered an anti-pattern in modern web development. UA strings can be spoofed, are increasingly frozen to prevent tracking, and do not reliably indicate capability. The recommended approach is feature detection: test whether a specific API exists in JavaScript (if ('serviceWorker' in navigator)) rather than inferring capability from the browser name. Legitimate uses for UA parsing: analytics, bot detection, logging, and serving different content variants based on detected device type.

Frequently Asked Questions

Why do all browsers include "Mozilla/5.0"?

Historical inertia. Early web servers served richer content only to Netscape (Mozilla). Internet Explorer added "Mozilla" to get the same content. All browsers followed suit. Today, "Mozilla/5.0" is a meaningless compatibility token in Chrome, Firefox, Safari, and Edge β€” it cannot identify any specific browser.

How do I parse User-Agents in Java?

Use the uap-java (ua-parser) library. Add the dependency, then Client c = new Parser().parse(uaString). Access c.userAgent.family (browser name), c.os.family (OS), and c.device.family (device type). The ua-parser project maintains a community-updated regex file covering thousands of UAs. Alternatively, use user-agent-utils for a simpler API.

What is the User-Agent Client Hints API?

User-Agent Client Hints (UA-CH) is a newer mechanism that replaces the monolithic UA string with structured, opt-in hints. Servers request specific information via the Accept-CH header; browsers send only what is requested. This reduces passive fingerprinting while still allowing legitimate UA detection. Chrome has been progressively "freezing" its UA string to standardize certain tokens and encourage migration to UA-CH.