req.xhr is a Boolean that is true when the request’s X-Requested-With header is XMLHttpRequest (compared case-insensitively). Express documents this as a signal that a client library such as jQuery issued the request.
Use it as a hint to choose JSON vs HTML in older apps. It is not a security check. Prefer req.accepts() with an Accept header for modern content negotiation. Read the raw header with req.get('X-Requested-With').
Syntax
Read req.xhr in your Express.js route handler. Start with this line.
Client declares preference.Accept: application/json is standard HTTP.
req.accepts picks a match. Returns the best type or falsy if none fit.
Respond accordingly. JSON, HTML, or 406 when nothing matches.
Why switch. Works with bare fetch and avoids a non-standard header.
Compatibility
Runtime Support
req.xhr is an Express.js request property. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only send headers; they do not expose req.xhr.
✓ Server · Express 4 & 5
req.xhr
Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.
ServerNode.js runtime API
Node.js18+ for Express 5 · 14+ typical for Express 4
Supported
BunExpress-compatible server apps
Supported
DenoVia npm compatibility / adapters
Supported
req.xhrExpress on Node.js
Bottom line: Study and run these examples with Node.js and the express package. Copy a snippet into a local file and start the server with node.
Frequently Asked Questions
It is a Boolean that is true when the X-Requested-With request header equals XMLHttpRequest (case-insensitive). Express documents this as a common signal from client libraries such as jQuery.
No. The Fetch API and many clients do not send X-Requested-With by default. You must add the header yourself if you rely on req.xhr.
No. Anyone can send or omit the header. Treat it as a convenience hint for response format, not authentication or authorization.
Prefer content negotiation with the Accept header and req.accepts('json') / req.accepts('html') for modern APIs.
No. It is a server-side Express property. Browsers only send headers; Express reads them.