Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9eb5ab4
chore: add support for OSSF scorecard reporting (#522)
inigomarquinez May 14, 2024
1c2f923
Added support for external parsers to bodyParser.json()
Nov 21, 2017
2f4913c
removed test dependency on json-bigint
Nov 21, 2017
6daefb5
reworked doc to describe json parser() func better
Nov 21, 2017
b740295
added parser() option and doc for .text()
Nov 21, 2017
cf3a8e3
added parser() option and doc for .raw()
Nov 21, 2017
2468f72
added parser() option and doc for .urlencoded()
Nov 21, 2017
d75adee
cleanup to satisfy linter
Nov 21, 2017
8224cda
added generic parser
Nov 21, 2017
a5adc82
converted json parser to use generic parser
Nov 21, 2017
c572330
converted raw parser to use generic parser
Nov 21, 2017
7390c89
converted text parser to use generic parser
Nov 21, 2017
f4f9d84
converted urlencoded parser to use generic parser
Nov 21, 2017
10eb0af
cleanup / fix linter warnings
Nov 21, 2017
67060ca
removed items from README
Nov 21, 2017
e6f6004
added bodyParser.generic() getter
Nov 21, 2017
07e0602
cleanup / fix linter warnings
Nov 21, 2017
bb7697a
fixed tests after rebase
sdellysse Apr 17, 2020
6a7c24d
satisfying linter
sdellysse Apr 17, 2020
bc7454b
Ref'd genParser via the bodyparser getter to signal how third party p…
sdellysse Apr 17, 2020
519f306
removed dep on object-assign, which didnt support node < 0.10
sdellysse Apr 17, 2020
cdf46f0
minor text cleanup
sdellysse Apr 17, 2020
8ac04fd
🔧 add debug script
ctcpip May 24, 2024
1a43910
🐛 fix object merging
ctcpip May 24, 2024
86804fe
🔥 clean up
ctcpip May 24, 2024
88f84bd
💚 remove node < 4 from CI
ctcpip May 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added parser() option and doc for .raw()
  • Loading branch information
Shawn Dellysse authored and ctcpip committed May 24, 2024
commit cf3a8e37a2e69becbf65d885381a5c31262e6e50
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ The `verify` 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.

##### parser

The `parser` option, if supplied, is used to transform the body of a request
before being set to `req.body`.

```javascript
parser(body) -> req.body
```

### bodyParser.text([options])

Returns middleware that parses all bodies as a string and only looks at
Expand Down
7 changes: 6 additions & 1 deletion lib/types/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function raw (options) {
: opts.limit
var type = opts.type || 'application/octet-stream'
var verify = opts.verify || false
var parser = opts.parser || defaultParser

if (verify !== false && typeof verify !== 'function') {
throw new TypeError('option verify must be function')
Expand All @@ -50,7 +51,7 @@ function raw (options) {
: type

function parse (buf) {
return buf
return parser(buf)
}

return function rawParser (req, res, next) {
Expand Down Expand Up @@ -90,6 +91,10 @@ function raw (options) {
}
}

function defaultParser (buf) {
return buf;
}

/**
* Get the simple type checker.
*
Expand Down
10 changes: 10 additions & 0 deletions test/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ describe('bodyParser.raw()', function () {
.expect(200, 'buf:746865207573657220697320746f6269', done)
})

it('should parse application/octet-stream with a custom parser', function (done) {
request(createServer({
parser: function (body) { return body.toString("utf8") }
}))
.post('/')
.set('Content-Type', 'application/octet-stream')
.send('the user is tobi')
.expect(200, '"the user is tobi"', done)
})

it('should 400 when invalid content-length', function (done) {
var rawParser = bodyParser.raw()
var server = createServer(function (req, res, next) {
Expand Down