New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to set a default, server wide response content-type ? #517
Comments
I would say add a fastify.addHook('onSend', function(request, reply, payload, done) {
reply.headers({'content-type': 'application/json'})
done()
}) |
@climba03003 Thanks. |
... more troubles found, see #518 :-| |
Then move the hook one more level upper. Use |
when I set this, the server hangs forever... incorrect code ?
|
You've missed done fn |
b/c it throws TS errors - I had it in there initially:
throws:
when I remove the If I suppress with |
you should write: fastify.addHook('preHandler', (request, reply, done) => {
reply.headers({'content-type': 'application/json; charset=utf-8'});
done();
}); |
ah, makes sense. just tried this and this works, unless the scenarios that I described in #3283 come into play. Most importantly: Thanks ! |
I will close it as your problem solved. For the other issue, serialization should only happen for the appropriate content type specified. So, this is why we have The reason for no reason already confirmed is bug from |
What are you trying to achieve, or the steps to reproduce?
Evaluating
fastify
in a few test set ups. Our use case scenarios are eithercontent-type: application/json
orcontent-type: text/plain
which are supported by default.It does appear though (please correct if wrong) that
fastify
guesses thecontent-type
of the response (based on the data type being returned) and automatically sets it, which can lead to incorrect results. I can override this in thereply
object, but would have to do this in every handler method...example 1: correct guess
example 2: incorrect guess
I know that if just an object
{foo: bar, bar: 'foo'}
would be passed into.send()
in example 2, it would then lead to the correct guess and setting ofcontent-type: application/json
, but often data comes from other sources and is already correctly prepped or encoded, hence we know for example that we needcontent-type: application/json
, no matter whatfastify
thinks.So, is there a server config option that allows to specify a default response
content-type
that is then always used byfastify
unless it is overridden by the reply object ?Context
Thanks.
The text was updated successfully, but these errors were encountered: