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

raw-body

Package Overview
Dependencies
Maintainers
2
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

raw-body

Get and validate the raw body of a readable stream.

  • 1.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
29M
decreased by-28.5%
Maintainers
2
Weekly downloads
 
Created

What is raw-body?

The raw-body npm package is used to obtain the raw body of an incoming stream and supports decoding, parsing, and handling of different encodings. It is commonly used in the context of HTTP server handling, where it can be used to read and parse request bodies before they are processed by request handlers or middleware.

What are raw-body's main functionalities?

Getting raw body from a stream

This code creates an HTTP server that uses raw-body to read the request body as a string. It takes into account the content length and encoding specified in the request headers.

const http = require('http');
const getRawBody = require('raw-body');

http.createServer((req, res) => {
  getRawBody(req, {
    length: req.headers['content-length'],
    encoding: 'utf8'
  }, function (err, string) {
    if (err) return res.end('Error');
    res.end('Received: ' + string);
  });
}).listen(3000);

Handling different encodings

This code demonstrates how to use raw-body to handle different text encodings by specifying the encoding option. The promise interface is used for asynchronous handling.

const getRawBody = require('raw-body');

function handleRequest(req) {
  return getRawBody(req, {
    encoding: 'utf8'
  }).then(body => {
    // body is now a string in utf8 encoding
  }).catch(err => {
    // handle error
  });
}

Limiting body size

This code shows how to limit the size of the request body using raw-body by setting a limit option, which can help prevent denial of service attacks or other resource exhaustion issues.

const getRawBody = require('raw-body');

function handleRequest(req) {
  return getRawBody(req, {
    limit: '1mb'
  }).then(body => {
    // body will not be larger than 1mb
  }).catch(err => {
    // handle error if body is too large
  });
}

Other packages similar to raw-body

FAQs

Package last updated on 13 Jun 2014

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