Express req.is() Method
What you’ll learn
- How to validate request payload types with
req.is(). - How to check JSON, form, and wildcard MIME patterns.
- How to return proper status codes for unsupported payloads.
- How
req.is()differs fromreq.accepts().
Usage syntax
javascript
req.is(type)
req.is(type1, type2, ...)1
Accept only JSON payloads
javascript
app.post('/api/orders', function (req, res) {
if (!req.is('application/json')) {
return res.status(415).send('JSON payload required');
}
res.send('Order accepted');
});2
Support JSON or form data
javascript
app.post('/submit', function (req, res) {
var type = req.is('json', 'urlencoded');
if (!type) return res.status(415).send('Unsupported content type');
res.send('Parsed as: ' + type);
});❓ FAQ
It checks whether the request Content-Type matches one or more MIME patterns.
It returns the matched content type string, null in some no-body contexts, or false when no match is found.
Yes. You can pass multiple arguments like req.is('json', 'application/*').
No. req.is() checks incoming Content-Type. Use req.accepts() for Accept negotiation.
Yes, it helps validate expected payload format before processing body data.
Did you know?
req.is() checks the request Content-Type and returns the matched type (or false when it does not match).
4 people found this page helpful
