A generic HTTP body parser for Deno
Generic body parser for Deno’s ServerRequest
body.
Features:
import { createBodyParser, JsonBodyParser } from "https://deno.land/x/body_parser/mod.ts";
const bodyParser = createBodyParser({
parsers: [new JsonBodyParser()],
});
// ...
// Request with Content-Type of application/json with {"x": 1}
const results = await bodyParser(req);
assertEquals(results, {
parser: "json",
value: {
x: 1,
},
});
createBodyParser
Creates the body parser using a list a parsers as an option.
The created body parser accepts a ServerRequest
from Deno’s HTTP server, and returns undefined
if no suitable parser is found (based on Content-Type
), or an object of { parser: string, value: any}
if one was found.
All parsers accept the follow options:
limit
: The limit in bytes of body to parsertype
: One of many strings, RegExps, of functions to check if the Content-Type
is suitable for the parserJsonBodyParser
(json
)A parser for JSON requests using application/json
.
Accepts the follow options (includiong the default ones listed above):
strict
: If enabled, only objects/arrays will be allowed to be parsed. Defaults to `falsereviver
: The second argument to JSON.parse
. You shouldn’t need this. Read more about reviversUrlencodedBodyParser
(urlencoded
)A parser for form requests using application/x-www-form-urlencoded
.
You can implement your own parsers by extending BodyParser
. You can see examples for the JSON parser or URL encoded parser.
Since Deno is evolving quickly, only the latest version is officially supported.
Please file feature requests and bugs at the issue tracker.