Skip to content

No way to disable charset in Content-Type header? #3490

@tomasruud

Description

@tomasruud

I'm developing an API that will replace another API implemented in another language. Because the clients for this API are implemented badly, they don't accept content types other than "application/json" this means that "application/json; charset=utf-8" breaks them.

Currently, I've been trying to use this middleware before my routes to prevent the charset from being appended:

app.use((req, res, next) => {
  res.setHeader('Content-Type', 'application/json');
  return next();
});

I usually end responses like this res.status(404).json('Not found'), but with the middleware I'm using, it doesn't seem to work. I've tried to look at the Express code, and from what I can tell, if I use the Express function set(), header() or type() the charset automatically gets appended. So when I use the native setHeader() function I thought that would work. When I use the native method, it doesn't seem like it should be overridden, because the res.json() function checks if the header is already set before trying to set it, which also uses the native getHeader() function.

Is there any way I can get around this, without changing how reponses are sent or am I doing something wrong?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions