|
| 1 | +<h3 id='express.text' class='h2'>express.text([options])</h3> |
| 2 | + |
| 3 | +<div class="doc-box doc-info" markdown="1"> |
| 4 | +This middleware is available in Express v4.17.0 onwards. |
| 5 | +</div> |
| 6 | + |
| 7 | +This is a built-in middleware function in Express. It parses incoming request |
| 8 | +payloads into a string and is based on |
| 9 | +[body-parser](/{{ page.lang }}/resources/middleware/body-parser.html). |
| 10 | + |
| 11 | +Returns middleware that parses all bodies as a string and only looks at requests |
| 12 | +where the `Content-Type` header matches the `type` option. This parser accepts |
| 13 | +any Unicode encoding of the body and supports automatic inflation of `gzip` and |
| 14 | +`deflate` encodings. |
| 15 | + |
| 16 | +A new `body` string containing the parsed data is populated on the `request` |
| 17 | +object after the middleware (i.e. `req.body`), or an empty object (`{}`) if |
| 18 | +there was no body to parse, the `Content-Type` was not matched, or an error |
| 19 | +occurred. |
| 20 | + |
| 21 | +<div class="doc-box doc-warn" markdown="1"> |
| 22 | +As `req.body`'s shape is based on user-controlled input, all properties and |
| 23 | +values in this object are untrusted and should be validated before trusting. |
| 24 | +For example, `req.body.trim()` may fail in multiple ways, for example |
| 25 | +stacking multiple parsers `req.body` may be from a different parser. Testing |
| 26 | +that `req.body` is a string before calling string methods is recommended. |
| 27 | +</div> |
| 28 | + |
| 29 | +The following table describes the properties of the optional `options` object. |
| 30 | + |
| 31 | +| Property | Description | Type | Default | |
| 32 | +|------------------|-----------------------------------------------------------------------|-------------|-----------------| |
| 33 | +| `defaultCharset` | Specify the default character set for the text content if the charset is not specified in the `Content-Type` header of the request. | String | `"utf-8"` | |
| 34 | +| `inflate` | Enables or disables handling deflated (compressed) bodies; when disabled, deflated bodies are rejected. | Boolean | `true` | |
| 35 | +| `limit` | Controls the maximum request body size. If this is a number, then the value specifies the number of bytes; if it is a string, the value is passed to the [bytes](https://www.npmjs.com/package/bytes) library for parsing. | Mixed | `"100kb"` | |
| 36 | +| `type` | This is used to determine what media type the middleware will parse. This option can be a string, array of strings, or a function. If not a function, `type` option is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) library and this can be an extension name (like `txt`), a mime type (like `text/plain`), or a mime type with a wildcard (like `*/*` or `text/*`). If a function, the `type` option is called as `fn(req)` and the request is parsed if it returns a truthy value. | Mixed | `"text/plain"` | |
| 37 | +| `verify` | This option, if supplied, is called as `verify(req, res, buf, encoding)`, where `buf` is a `Buffer` of the raw request body and `encoding` is the encoding of the request. The parsing can be aborted by throwing an error. | Function | `undefined` | |
0 commit comments