-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
doc, http: Type specification incomplete for ServerResponse.getHeader() #13825
Comments
@Flarna - if I understand your question correctly, you are saying that the headers are stored as objects before sent to the wire, so I guess they are stored as strings by stripping them from the object in writeHead method . |
It's quite a while till I submitted this. Here is a small reproducer; hope I remember correct on the actual use case...
this prints:
So if The use of |
@Flarna - I think I got your point now. Probably the Consequently, if you do a dummy setHeader first, subsequent writeHeads are stored in the internal field, and are accessible through getHeader: #cat 13825.js const h = require('http')
const n = require('net')
h.createServer((q, s) => {
const bodys = 'hello world'
const bodyw = '<html>hello</html>'
if(process.argv[2])
s.setHeader('Content-Length', bodys.length)
s.writeHead(200, { 'Content-Length': bodyw.length, 'Content-Type': 'text/html' })
console.log(s.getHeader('Content-Length'))
console.log(s.getHeader('Content-Type'))
s.end(bodyw)
}).listen(8000, () => {
h.get('http://localhost:8000')
}) #node 13825.js Hope this helps. /cc @nodejs/http for expert opinion. |
@Flarna I think the document needs to be changed. Would you like to make a PR to make the document more perfect? |
@MoonBall ok, will do so once I have some poetic moments to formulate this in a reasonable way. |
http.setHeader() coerces input values. http.getHeader() returns the type as passed to setHeader(). PR-URL: #19902 Fixes: #13825 Reviewed-By: Chen Gang <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: James M Snell <[email protected]>
http.setHeader() coerces input values. http.getHeader() returns the type as passed to setHeader(). PR-URL: nodejs#19902 Fixes: nodejs#13825 Reviewed-By: Chen Gang <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: James M Snell <[email protected]>
ServerResponse.setHeader()
specifies name asstring
and value asstring | string[]
.ServerResponse.writeHead()
allows an object for headers (key is header name, value is header value). All samples usestring
for keys andstring | string[]
for values - except one which usesBuffer.byteLength(body)
which is anumber
(the sample works fine).ServerResponse.getHeader()
specifies to return astring
.Actually
getHeader()
returns what as passed in so it should be at leaststring | string[]
.But as
setHeader()
andwriteHead()
actually allow any stringifyable type you get also such type back as the conversion tostring
happens when writing the outgoing stream not during storing the header.Not sure here if this is worth a change; and if yes should it be in doc or code?
I would guess the same is applicable to
ClientRequest
.The text was updated successfully, but these errors were encountered: