Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koa-body

Package Overview
Dependencies
Maintainers
2
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-body

A Koa body parser middleware. Supports multipart, urlencoded and JSON request bodies.

  • 6.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
340K
increased by6.18%
Maintainers
2
Weekly downloads
 
Created

What is koa-body?

koa-body is a middleware for Koa that parses incoming request bodies in various formats, including JSON, URL-encoded, and multipart forms. It simplifies handling file uploads and form submissions in Koa applications.

What are koa-body's main functionalities?

JSON Body Parsing

This feature allows you to parse JSON bodies from incoming requests. The middleware automatically parses the JSON and makes it available in `ctx.request.body`.

const Koa = require('koa');
const koaBody = require('koa-body');
const app = new Koa();

app.use(koaBody());

app.use(async ctx => {
  if (ctx.method === 'POST') {
    ctx.body = `Received JSON data: ${JSON.stringify(ctx.request.body)}`;
  } else {
    ctx.body = 'Send a POST request with JSON data';
  }
});

app.listen(3000);

URL-encoded Body Parsing

This feature allows you to parse URL-encoded bodies from incoming requests. The middleware automatically parses the URL-encoded data and makes it available in `ctx.request.body`.

const Koa = require('koa');
const koaBody = require('koa-body');
const app = new Koa();

app.use(koaBody({ urlencoded: true }));

app.use(async ctx => {
  if (ctx.method === 'POST') {
    ctx.body = `Received URL-encoded data: ${JSON.stringify(ctx.request.body)}`;
  } else {
    ctx.body = 'Send a POST request with URL-encoded data';
  }
});

app.listen(3000);

Multipart Form Parsing

This feature allows you to parse multipart form data, which is commonly used for file uploads. The middleware automatically parses the multipart data and makes it available in `ctx.request.files`.

const Koa = require('koa');
const koaBody = require('koa-body');
const app = new Koa();

app.use(koaBody({ multipart: true }));

app.use(async ctx => {
  if (ctx.method === 'POST') {
    const files = ctx.request.files;
    ctx.body = `Received files: ${JSON.stringify(files)}`;
  } else {
    ctx.body = 'Send a POST request with multipart form data';
  }
});

app.listen(3000);

Other packages similar to koa-body

Keywords

FAQs

Package last updated on 29 Oct 2022

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc