You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -33,12 +34,16 @@ You can also use the `hasContentTypeParser` API to find if a specific content ty
33
34
34
35
```js
35
36
if (!fastify.hasContentTypeParser('application/jsoff')){
36
-
fastify.addContentTypeParser('application/jsoff', function (req, done) {
37
-
// Code to parse request body/payload for the given content type
37
+
fastify.addContentTypeParser('application/jsoff', function (request, payload, done) {
38
+
jsoffParser(payload, function (err, body) {
39
+
done(err, body)
40
+
})
38
41
})
39
42
}
40
43
```
41
44
45
+
**Notice**: The old syntaxes `function(req, done)` and `async function(req)` for the parser are still supported but they are deprecated.
46
+
42
47
#### Body Parser
43
48
You can parse the body of a request in two ways. The first one is shown above: you add a custom content type parser and handle the request stream. In the second one, you should pass a `parseAs` option to the `addContentTypeParser` API, where you declare how you want to get the body. It could be of type `'string'` or `'buffer'`. If you use the `parseAs` option, Fastify will internally handle the stream and perform some checks, such as the [maximum size](./Server.md#factory-body-limit) of the body and the content length. If the limit is exceeded the custom parser will not be invoked.
44
49
```js
@@ -52,7 +57,6 @@ fastify.addContentTypeParser('application/json', { parseAs: 'string' }, function
52
57
}
53
58
})
54
59
```
55
-
As you can see, now the function signature is `(req, body, done)` instead of `(req, done)`.
56
60
57
61
See [`example/parser.js`](../examples/parser.js) for an example.
58
62
@@ -63,10 +67,10 @@ See [`example/parser.js`](../examples/parser.js) for an example.
63
67
#### Catch-All
64
68
There are some cases where you need to catch all requests regardless of their content type. With Fastify, you can just use the `'*'` content type.
65
69
```js
66
-
fastify.addContentTypeParser('*', function (req, done) {
70
+
fastify.addContentTypeParser('*', function (request, payload, done) {
67
71
var data =''
68
-
req.on('data', chunk=> { data += chunk })
69
-
req.on('end', () => {
72
+
payload.on('data', chunk=> { data += chunk })
73
+
payload.on('end', () => {
70
74
done(null, data)
71
75
})
72
76
})
@@ -77,7 +81,7 @@ Using this, all requests that do not have a corresponding content type parser wi
77
81
This is also useful for piping the request stream. You can define a content parser like:
78
82
79
83
```js
80
-
fastify.addContentTypeParser('*', function (req, done) {
84
+
fastify.addContentTypeParser('*', function (request, payload, done) {
81
85
done()
82
86
})
83
87
```
@@ -86,7 +90,7 @@ and then access the core HTTP request directly for piping it where you want:
86
90
87
91
```js
88
92
app.post('/hello', (request, reply) => {
89
-
reply.send(request.req)
93
+
reply.send(request.raw)
90
94
})
91
95
```
92
96
@@ -96,8 +100,8 @@ Here is a complete example that logs incoming [json line](http://jsonlines.org/)
**Notice:** in the [onRequest](#onRequest) hook, `request.body` will always be `null`, because the body parsing happens before the [preValidation](#preValidation) hook.
55
55
56
56
### preParsing
57
+
58
+
If you are using the `preParsing` hook, you can transform the request payload stream before it is parsed. It receives the request and reply objects as other hooks, and a stream with the current request payload.
59
+
60
+
If it returns a value (via `return` or via the callback function), it must return a stream.
61
+
62
+
For instance, you can uncompress the request body:
**Notice:** in the [preParsing](#preParsing) hook, `request.body` will always be `null`, because the body parsing happens before the [preValidation](#preValidation) hook.
73
80
81
+
**Notice:** you should also add `receivedEncodedLength` property to the returned stream. This property is used to correctly match the request payload with the `Content-Length` header value. Ideally, this property should be updated on each received chunk.
82
+
83
+
**Notice**: The old syntaxes `function(request, reply, done)` and `async function(request, reply)` for the parser are still supported but they are deprecated.
**Notice:** in the [preValidation](#preValidation) hook, `request.body` will always be `null`, because the body parsing happens before the [preValidation](#preHandler) hook.
From Fastify v3, the behavior of `preParsing` hook will change slightly in order to support request payload manipulation.
124
+
125
+
The hook now takes an additional argument, `payload`, and therefore the new hook signature is `fn(request, reply, payload, done)` or `async fn(request, reply, payload)`.
126
+
127
+
The hook can optionally return a new stream via `done(null, stream)` or returning the stream in case of async functions.
128
+
129
+
If the hook returns a new stream, it will be used instead of the original one in following hooks. A sample use case for this is handling compressed requests.
130
+
131
+
The new stream should add the `receivedEncodedLength` property to the stream that should reflect the actual data size received from the client. For instance, in compressed request it should be the size of the compressed payload.
132
+
This property can (and should) be dynamically updated during `data` events.
133
+
134
+
The old syntax of Fastify v2 without payload it is supported but it is deprecated.
From Fastify v3, the behavior of `onRoute` and `onRegister` hooks will change slightly in order to support hook encapsulation.
124
139
125
140
-`onRoute` - The hook will be called asynchronously, in v1/v2 it's called as soon as a route is registered. This means that if you want to use it, you should register this hook as soon as possible in your code.
126
141
-`onRegister` - Same as the onRoute hook, the only difference is that now the very first call will no longer be the framework itself, but the first registered plugin
127
142
143
+
### Changed Content Type Parser syntax ([#2286](https://github.com/fastify/fastify/pull/2286))
144
+
145
+
In Fastify v3 the Content Type Parsers have now a single signature for parsers.
146
+
147
+
The new signatures is `fn(request, payload, done)` or `async fn(request, payload)`. Note that `request` is now a fastify request, not an `IncomingMessage`.
148
+
The payload is by default a stream. If the `parseAs` option is used in `addContentTypeParser`, then `payload` reflects the option value (string or buffer).
149
+
150
+
The old signatures `fn(req, [done])` or `fn(req, payload, [done])` (where `req` is `IncomingMessage`) are still supported but deprecated.
151
+
128
152
### Changed TypeScript support
129
153
130
154
The type system was changed in Fastify version 3. The new type system introduces generic constraining and defaulting, plus a new way to define schema types such as a request body, querystring, and more!
0 commit comments