Usage in Deno
import { IncomingMessage } from "node:http";
An IncomingMessage
object is created by Server or ClientRequest and passed as the first argument to the 'request'
and 'response'
event respectively. It may be used to
access response
status, headers, and data.
Different from its socket
value which is a subclass of stream.Duplex
, the IncomingMessage
itself extends stream.Readable
and is created separately to
parse and emit the incoming HTTP headers and payload, as the underlying socket
may be reused multiple times in case of keep-alive.
IncomingMessage(socket: Socket)
aborted: boolean
The message.aborted
property will be true
if the request has
been aborted.
complete: boolean
The message.complete
property will be true
if a complete HTTP message has
been received and successfully parsed.
This property is particularly useful as a means of determining if a client or server fully transmitted a message before a connection was terminated:
const req = http.request({ host: '127.0.0.1', port: 8080, method: 'POST', }, (res) => { res.resume(); res.on('end', () => { if (!res.complete) console.error( 'The connection was terminated while the message was still being sent'); }); });
connection: Socket
Alias for message.socket
.
The request/response headers object.
Key-value pairs of header names and values. Header names are lower-cased.
// Prints something like: // // { 'user-agent': 'curl/7.22.0', // host: '127.0.0.1:8000', // accept: '*' } console.log(request.headers);
Duplicates in raw headers are handled in the following ways, depending on the header name:
- Duplicates of
age
,authorization
,content-length
,content-type
,etag
,expires
,from
,host
,if-modified-since
,if-unmodified-since
,last-modified
,location
,max-forwards
,proxy-authorization
,referer
,retry-after
,server
, oruser-agent
are discarded. To allow duplicate values of the headers listed above to be joined, use the optionjoinDuplicateHeaders
in request and createServer. See RFC 9110 Section 5.3 for more information. set-cookie
is always an array. Duplicates are added to the array.- For duplicate
cookie
headers, the values are joined together with;
. - For all other headers, the values are joined together with
,
.
headersDistinct: Dict<string[]>
Similar to message.headers
, but there is no join logic and the values are
always arrays of strings, even for headers received just once.
// Prints something like: // // { 'user-agent': ['curl/7.22.0'], // host: ['127.0.0.1:8000'], // accept: ['*'] } console.log(request.headersDistinct);
httpVersion: string
In case of server request, the HTTP version sent by the client. In the case of
client response, the HTTP version of the connected-to server.
Probably either '1.1'
or '1.0'
.
Also message.httpVersionMajor
is the first integer and message.httpVersionMinor
is the second.
httpVersionMajor: number
httpVersionMinor: number
method: string | undefined
Only valid for request obtained from Server.
The request method as a string. Read only. Examples: 'GET'
, 'DELETE'
.
rawHeaders: string[]
The raw request/response headers list exactly as they were received.
The keys and values are in the same list. It is not a list of tuples. So, the even-numbered offsets are key values, and the odd-numbered offsets are the associated values.
Header names are not lowercased, and duplicates are not merged.
// Prints something like: // // [ 'user-agent', // 'this is invalid because there can be only one', // 'User-Agent', // 'curl/7.22.0', // 'Host', // '127.0.0.1:8000', // 'ACCEPT', // '*' ] console.log(request.rawHeaders);
rawTrailers: string[]
The raw request/response trailer keys and values exactly as they were
received. Only populated at the 'end'
event.
The net.Socket
object associated with the connection.
With HTTPS support, use request.socket.getPeerCertificate()
to obtain the
client's authentication details.
This property is guaranteed to be an instance of the net.Socket
class,
a subclass of stream.Duplex
, unless the user specified a socket
type other than net.Socket
or internally nulled.
statusCode: number | undefined
Only valid for response obtained from ClientRequest.
The 3-digit HTTP response status code. E.G. 404
.
statusMessage: string | undefined
Only valid for response obtained from ClientRequest.
The HTTP response status message (reason phrase). E.G. OK
or Internal Server Error
.
trailers: Dict<string>
The request/response trailers object. Only populated at the 'end'
event.
trailersDistinct: Dict<string[]>
Similar to message.trailers
, but there is no join logic and the values are
always arrays of strings, even for headers received just once.
Only populated at the 'end'
event.
url: string | undefined
Only valid for request obtained from Server.
Request URL string. This contains only the URL that is present in the actual HTTP request. Take the following request:
GET /status?name=ryan HTTP/1.1 Accept: text/plain
To parse the URL into its parts:
new URL(`http://${process.env.HOST ?? 'localhost'}${request.url}`);
When request.url
is '/status?name=ryan'
and process.env.HOST
is undefined:
$ node > new URL(`http://${process.env.HOST ?? 'localhost'}${request.url}`); URL { href: 'http://localhost/status?name=ryan', origin: 'http://localhost', protocol: 'http:', username: '', password: '', host: 'localhost', hostname: 'localhost', port: '', pathname: '/status', search: '?name=ryan', searchParams: URLSearchParams { 'name' => 'ryan' }, hash: '' }
Ensure that you set process.env.HOST
to the server's host name, or consider replacing this part entirely. If using req.headers.host
, ensure proper
validation is used, as clients may specify a custom Host
header.
destroy(error?: Error): this
Calls destroy()
on the socket that received the IncomingMessage
. If error
is provided, an 'error'
event is emitted on the socket and error
is passed
as an argument to any listeners on the event.
setTimeout(msecs: number,callback?: () => void,): this
Calls message.socket.setTimeout(msecs, callback)
.