
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
fastify-simple-form
Advanced tools
Adds content type parser for the application/x-www-form-urlencoded and multipart/form-data content types
Fastify plugin that adds content type parser for the application/x-www-form-urlencoded
and/or multipart/form-data
types.
Essentially a tiny wrapper around busboy, that parses application/x-www-form-urlencoded
and/or multipart/form-data
content types and attaches associated fields to request.body
.
NB! This plugin does not handle files
, these get simply discarded as described here.
npm install fastify-simple-form
Although this package includes typings for the plugin itself, you must install ones for node.js and busboy manually:
npm install @types/node @types/busboy --save-dev
fastify.register(require('fastify-simple-form'), {
multipart: true, // Enable parsing for `multipart/form-data`, default: true
urlencoded: false, // Disable parsing for `application/x-www-form-urlencoded`, default: true
});
This plugin has no effect when both options above are set to false
!
Accepts identical options to those of busboy constructor (with the exception of headers
), e.g.:
fastify.register(require('fastify-simple-form'), {
defCharset: 'utf8',
limits: {
fieldNameSize: 100, // Max field name size (in bytes), default: 100
fieldSize: 1000000, // Max field value size (in bytes), default: 1MB
fields: 10, // Max number of non-file fields, default: Infinity
},
});
Given server & handler:
import Fastify from 'fastify';
import parseForm from 'fastify-simple-form';
const fastify = Fastify();
fastify.register(parseForm);
fastify.post(
'/token',
{
schema: {
body: {
type: 'object',
properties: {
username: {
type: 'string',
},
password: {
type: 'string',
},
client_id: {
type: 'string',
format: 'uuid',
},
client_secret: {
type: 'string',
},
refresh_token: {
type: 'string',
format: 'uuid',
},
grant_type: {
type: 'string',
enum: ['authorization_code', 'refresh_token'],
},
},
required: ['grant_type'],
},
},
},
(request, reply) => {
reply.send(request.body);
},
);
fastify.listen(3000);
These requests would succeed:
curl \
-F "client_id=e52b2864-7611-4f26-94e4-d13f7039f25d" \
-F "client_secret=rGGb45-awp0Q9X2yP3CxwhP8HhUY8uW1" \
-F "refresh_token=f5e6ca6e-e2cf-4130-9b3f-26727ca11f78" \
-F "grant_type=refresh_token" \
localhost:3000/token
curl \
-d "client_id=e52b2864-7611-4f26-94e4-d13f7039f25d" \
-d "client_secret=rGGb45-awp0Q9X2yP3CxwhP8HhUY8uW1" \
-d "refresh_token=f5e6ca6e-e2cf-4130-9b3f-26727ca11f78" \
-d "grant_type=refresh_token" \
localhost:3000/token
Response:
{
"client_id": "e52b2864-7611-4f26-94e4-d13f7039f25d",
"client_secret": "rGGb45-awp0Q9X2yP3CxwhP8HhUY8uW1",
"refresh_token": "f5e6ca6e-e2cf-4130-9b3f-26727ca11f78",
"grant_type": "refresh_token"
}
While these won't pass the schema validation
curl \
-F "username=jon" \
-F "password=snow" \
-F "grant_type=password" \
localhost:3000/token
curl \
-d "username=jon" \
-d "password=snow" \
-d "grant_type=password" \
localhost:3000/token
Response
{
"statusCode": 400,
"error": "Bad Request",
"message": "body.grant_type should be equal to one of the allowed values"
}
FAQs
Fastify plugin that adds content type parser for the application/x-www-form-urlencoded and/or multipart/form-data types
We found that fastify-simple-form demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.