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

res.send(Buffer.from(someString)) overwriting Content-Type response header #4138

Closed
dylan-hart opened this issue Dec 21, 2019 · 3 comments
Closed

Comments

@dylan-hart
Copy link

I've got this express (4.17.1) app running on Node.js (12.14.0):

const express = require('express')
const https = require('https')
const http = require('http')

const app = express()

const JSON_STRING = `{
    "associatedApplications": [
        {
            "applicationId": "abcdef"
        }
    ]
}`

// Serve .json file.
app.use('/my-json.json', (req, res, next) => {
    res.set('Content-Type', 'application/json')
    res.send(Buffer.from(JSON_STRING))
})

// Configure SSL.
const https_options = {
  key: ...,
  cert: ...,
  ca: [
     ...
  ]
}

// Make https server.
const https_server = https.createServer(https_options, app)
https_server.listen(443)

// Make http server.
const http_server = http.createServer(app)
http_server.listen(80)

In serving the .json file, I am trying to set the Content-Type response header to application/json, but Express appears to be overwriting it with application/json; charset=utf-8.

This appears to be normal behavior usually, but the docs and this previous issue seem to say that Express shouldn't overwrite the Content-Type response header when res.send() is passed a Buffer object.

Am I missing something, or is the documentation incorrect? If the documentation is incorrect and this is normal behavior, is there another way I can prevent Express from automatically overwriting the Content-Type response header?

For extra context: this is an issue related to configuring an application's publisher domain in Azure.

@dylan-hart dylan-hart changed the title res.send(Buffer.from(someString)) overriding Content-Type response header res.send(Buffer.from(someString)) overwriting Content-Type response header Dec 21, 2019
@dougwilson
Copy link
Contributor

I believe this is not an override like the linked issue, but is a feature of using res.set instead of res.setHeader where res.set will add the standard charset if not supplied where res.setHeader will use your header as-is.

@dylan-hart
Copy link
Author

Thanks for your input. Using res.setHeader('Content-Type', 'application/json') seems to still produce application/json; charset=utf-8, but I will keep playing around with it and see if I can come up with something.

@dougwilson
Copy link
Contributor

Ok. I will close for now until there is a reproduction we can look into to determine what is going wrong. Here is a terminal session of me trying to reproduce the issue. Perhaps you can provide something similar? Not sure if maybe there is something else at play in your setup adding the charset or something.

$ npm i express
+ [email protected]
added 50 packages from 37 contributors and audited 126 packages in 3.886s
found 0 vulnerabilities

$ node -v
v12.14.0

$ cat app.js
const express = require('express')
const http = require('http')

const app = express()

const JSON_STRING = `{
    "associatedApplications": [
        {
            "applicationId": "abcdef"
        }
    ]
}`

// Serve .json file.
app.use('/my-json.json', (req, res, next) => {
    res.setHeader('Content-Type', 'application/json')
    res.send(Buffer.from(JSON_STRING))
})

// Make http server.
const http_server = http.createServer(app)
http_server.listen(80)

$ node app &
[1] 1169

$ curl -is http://localhost/my-json.json
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json
Content-Length: 99
ETag: W/"63-IYHmhtDM3p6rsi4qSsHF0skYiXA"
Date: Sat, 21 Dec 2019 15:59:16 GMT
Connection: keep-alive

{
    "associatedApplications": [
        {
            "applicationId": "abcdef"
        }
    ]
}

You can see in the cURL output, the response header is just Content-Type: application/json

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

2 participants