Skip to content

Commit b48826f

Browse files
authored
docs: update Reply.send() documentation for string serialization (#6466)
1 parent e1e4fe7 commit b48826f

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

docs/Reference/Reply.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -671,9 +671,20 @@ fastify.get('/json', options, function (request, reply) {
671671
If you pass a string to `send` without a `Content-Type`, it will be sent as
672672
`text/plain; charset=utf-8`. If you set the `Content-Type` header and pass a
673673
string to `send`, it will be serialized with the custom serializer if one is
674-
set, otherwise, it will be sent unmodified (unless the `Content-Type` header is
675-
set to `application/json; charset=utf-8`, in which case it will be
676-
JSON-serialized like an object — see the section above).
674+
set, otherwise, it will be sent unmodified.
675+
676+
> **Note:** Even when the `Content-Type` header is set to `application/json`,
677+
> strings are sent unmodified by default. To serialize a string as JSON, you
678+
> must set a custom serializer:
679+
680+
```js
681+
fastify.get('/json-string', async function (request, reply) {
682+
reply
683+
.type('application/json; charset=utf-8')
684+
.serializer(JSON.stringify)
685+
.send('Hello') // Returns "Hello" (JSON-encoded string)
686+
})
687+
```
677688
```js
678689
fastify.get('/json', options, function (request, reply) {
679690
reply.send('plain string')

0 commit comments

Comments
 (0)