Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

495
Visualizações
How to set content-type when doing res.send()?

At the end of route function, when I do something like this.

res.send(JSON.stringify({...}));

I would like the content-type of response to be set as "text/plain", but it is "text/html". Is there way to set it explicitly? I search the document but no clue yet.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

setHeader before sending: https://nodejs.org/api/http.html#http_response_setheader_name_value

res.setHeader('content-type', 'text/plain');
res.send(JSON.stringify({...}));
over 4 years ago · Santiago Trujillo Relatório

0

There are a few ways to achieve this, the cleanest/simplest of which is probably (assuming the use of express, based on the tag; see final answer option if not using expressjs):

res.type('txt');
res.send(JSON.stringify({...}));

That's using res.type, which gives a number of shorthands, as well as being able to set explicit mime types, e.g. (among many others):

res.type('.html')            // => 'text/html'
res.type('html')             // => 'text/html'
res.type('json')             // => 'application/json'
res.type('application/json') // => 'application/json'
res.type('png')              // => 'image/png'
res.type('mp3')              // => 'audio/mp3'

As of this writing, this is implemented in terms of res.set, which could also be called directly:

res.set('content-type', 'text/plain');
res.send(JSON.stringify({...}));

These are the officially documented ways of performing this functionality (except that also res.header is an alias for res.set). However, it appears that the response objects are derived from http.ServerResponse, and thus also have (and use, in the implementation) the setHeader method from that, which has the same usage as res.set, without quite all of the same functionality as res.set (some checks aren't done, etc.)... But it does get the basic job done, and so it's entirely possible one might find code in the wild that would solve your this question's goals as (especially if it's not an express application, but using the http package, instead):

res.setHeader('Content-Type', 'text/plain');
res.send(JSON.stringify({...}));

(As a side-note: per RFC 7230 (formerly in RFC 2616), the header names are case insensitive. That said, the Content-Type capitalization is what's used in RFC 7231, which defines its meaning and usage, and is what res.type will use. That said, I've seen at least some examples of res.set being used with all-lower-case header names, so I thought I'd show that both are possible.)

over 4 years ago · Santiago Trujillo Relatório

0

You can try res.set to set a Content-Type header, like so:

res.set('content-type', 'text/plain');
res.send(JSON.stringify({...}));
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda