Skip to content
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

Closed
petef19 opened this issue Aug 25, 2021 · 10 comments
Closed

Is it possible to set a default, server wide response content-type ? #517

petef19 opened this issue Aug 25, 2021 · 10 comments

Comments

@petef19
Copy link

petef19 commented Aug 25, 2021

What are you trying to achieve, or the steps to reproduce?

Evaluating fastify in a few test set ups. Our use case scenarios are either content-type: application/json or content-type: text/plain which are supported by default.

It does appear though (please correct if wrong) that fastify guesses the content-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 the reply object, but would have to do this in every handler method...

example 1: correct guess

async handler(request: FastifyRequest, reply: FastifyReply)
{
  // `fastify` will guess correctly `content-type: text/plain`
  reply.code(200).send('my text');
}

example 2: incorrect guess

async handler(request: FastifyRequest, reply: FastifyReply)
{
  // `fastify` guessed "incorrectly" `content-type: text/plain`
  reply.code(200).send(JSON.stringify({foo: bar, bar: 'foo'}));

  // hence will have to manually override
  reply
    .code(200)
    .headers({'content-type': 'application/json'})
    .send(JSON.stringify({foo: bar, bar: 'foo'}));
}

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 of content-type: application/json, but often data comes from other sources and is already correctly prepped or encoded, hence we know for example that we need content-type: application/json, no matter what fastify thinks.

So, is there a server config option that allows to specify a default response content-type that is then always used by fastify unless it is overridden by the reply object ?

Context

  • node version: 14.6.0
  • fastify version: 3.20.2
  • os: Win 10 x64

Thanks.

@petef19 petef19 changed the title Is it possible to set a default response content-type ? Is it possible to set a default, server wide response content-type ? Aug 25, 2021
@climba03003
Copy link
Member

climba03003 commented Aug 26, 2021

I would say add a onSend hook is enough for your use-case..

fastify.addHook('onSend', function(request, reply, payload, done) {
  reply.headers({'content-type': 'application/json'})
  done()
})

@petef19
Copy link
Author

petef19 commented Aug 28, 2021

@climba03003
this does work to set a pre-defined content-type header for every reply, but unfortunately it seems this can then NOT be overridden on a case by case basis in the handler function, which is what we need... is there a way to achieve that ?

Thanks.

@petef19
Copy link
Author

petef19 commented Aug 28, 2021

... more troubles found, see #518 :-|

@climba03003
Copy link
Member

Then move the hook one more level upper. Use preHandler hook, so it can be override on handler.

@petef19
Copy link
Author

petef19 commented Aug 28, 2021

Then move the hook one more level upper. Use preHandler hook, so it can be override on handler.

when I set this, the server hangs forever... incorrect code ?

fastify.addHook('preHandler', (request, reply, payload) => {
    reply.headers({'content-type': 'application/json; charset=utf-8'});
});

@L2jLiga
Copy link
Member

L2jLiga commented Aug 28, 2021

when I set this, the server hangs forever... incorrect code ?

You've missed done fn

@petef19
Copy link
Author

petef19 commented Aug 28, 2021

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:

fastify.addHook('preHandler', (request, reply, payload, done) => {
    reply.headers({'content-type': 'application/json; charset=utf-8'});
    done();
});

throws:

TS2769: No overload matches this call.   
The last overload gave the following error.     
Argument of type '"preHandler"' is not assignable to parameter of type '"onClose"'.

when I remove the done param, all of this goes away, but then it doesn't work... (see above)

If I suppress with @ts-ignore then the TS error obviously does not block, but fastify server throws 500 response...

@zekth
Copy link
Member

zekth commented Aug 28, 2021

preHandler hook does not have payload parameter: https://www.fastify.io/docs/latest/Hooks/#prehandler

you should write:

fastify.addHook('preHandler', (request, reply, done) => {
    reply.headers({'content-type': 'application/json; charset=utf-8'});
    done();
});

@petef19
Copy link
Author

petef19 commented Aug 28, 2021

preHandler hook does not have payload parameter: https://www.fastify.io/docs/latest/Hooks/#prehandler

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:
when content-type is set to default to text/plain; charset=utf-8 but object is returned in response, fastify does not auto-serialize and the server hangs forever... two notes:
(1) server should never hang forever - even on user error
(2) fastify does auto-serialize when application/json is used (or auto guessed by fastify) - in case of errors w/ text/plain type auto-serialize could be attempted to avoid not returning anything and most importantly not hang forever

Thanks !

@climba03003
Copy link
Member

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 addContentTypeParser and reply.serializer.

The reason for no reason already confirmed is bug from fastify which is not sending the appropriate error. And it actually have server error (not the appropriate one) for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants