HttpContext
Class: HttpContext
Defined in: packages/core/src/contexts/httpcontext.ts:47
The HttpContext
It has similar properties than URL
See
https://developer.mozilla.org/en-US/docs/Web/API/URL_API
Constructors
Constructor
new HttpContext(
hostname,method,uri,protocol?,port?,headers?):HttpContext
Defined in: packages/core/src/contexts/httpcontext.ts:93
Create a new HttpContext
Parameters
hostname
string
the request hostname
method
the HTTP method
uri
string
the request URI
protocol?
"http" | "https" = "http"
the protocol
port?
string | number = "80"
the port number
headers?
{[key: string]: string | string[]; }
the request headers
Returns
HttpContext
Properties
body
body:
Buffer<ArrayBufferLike> |Readable
Defined in: packages/core/src/contexts/httpcontext.ts:73
clientCertificate?
optionalclientCertificate?:ClientCertificate
Defined in: packages/core/src/contexts/httpcontext.ts:78
Client certificate presented with the request (mTLS), if any
clientIp
clientIp:
string
Defined in: packages/core/src/contexts/httpcontext.ts:65
Get the client ip if available
cookies
cookies:
any
Defined in: packages/core/src/contexts/httpcontext.ts:74
headers
headers:
HeadersRequest
Defined in: packages/core/src/contexts/httpcontext.ts:70
host
host:
string
Defined in: packages/core/src/contexts/httpcontext.ts:72
hostname
hostname:
string
Defined in: packages/core/src/contexts/httpcontext.ts:53
Hostname
Example
test.webda.io
method
method:
HttpMethodType
Defined in: packages/core/src/contexts/httpcontext.ts:57
HTTP Method
origin
origin:
string
Defined in: packages/core/src/contexts/httpcontext.ts:71
path
path:
string
Defined in: packages/core/src/contexts/httpcontext.ts:66
port
port:
string
Defined in: packages/core/src/contexts/httpcontext.ts:69
prefix
prefix:
string=""
Defined in: packages/core/src/contexts/httpcontext.ts:83
URI prefix in case it is exposed through something that prefix the uri
protocol
protocol:
"http:"|"https:"
Defined in: packages/core/src/contexts/httpcontext.ts:68
search
search:
string
Defined in: packages/core/src/contexts/httpcontext.ts:67
uri
uri:
string
Defined in: packages/core/src/contexts/httpcontext.ts:61
Pathname
Methods
getAbsoluteUrl()
getAbsoluteUrl(
uri?):string
Defined in: packages/core/src/contexts/httpcontext.ts:465
Return the absolute url of the request, or of an application uri
An application uri is relative to the prefix, so the prefix is added to it
Parameters
uri?
string
to return absolute url from, the request uri by default
Returns
string
the result string
getClientCertificate()
getClientCertificate():
ClientCertificate
Defined in: packages/core/src/contexts/httpcontext.ts:169
Get the client certificate presented with the request
Check verified before trusting it: an unverified certificate was not checked against a trusted CA
Returns
the certificate, undefined when none was presented
getClientIp()
getClientIp():
string
Defined in: packages/core/src/contexts/httpcontext.ts:149
Get the client ip
Returns
string
the result string
getCookies()
getCookies():
any
Defined in: packages/core/src/contexts/httpcontext.ts:225
Get cookies
Returns
any
the result
getHeader()
getHeader(
name,def?):string|string[]
Defined in: packages/core/src/contexts/httpcontext.ts:402
Get header value
Parameters
name
string
the name to use
def?
string
the default value
Returns
string | string[]
the list of results
getHeaders()
getHeaders():
Readonly<IncomingHttpHeaders>
Defined in: packages/core/src/contexts/httpcontext.ts:392
Get HTTP Headers
Returns
Readonly<IncomingHttpHeaders>
the result
getHost()
getHost():
string
Defined in: packages/core/src/contexts/httpcontext.ts:258
Return hostname and port
Returns
string
the result string
getHostName()
getHostName():
string
Defined in: packages/core/src/contexts/httpcontext.ts:274
Get the hostname
Returns
string
the result string
getHref()
getHref():
string
Defined in: packages/core/src/contexts/httpcontext.ts:178
Returns
string
the result string
See
https://developer.mozilla.org/en-US/docs/Web/API/URL/href
getMethod()
getMethod():
HttpMethodType
Defined in: packages/core/src/contexts/httpcontext.ts:282
Get HTTP Method used
Returns
the result
getOrigin()
getOrigin():
string
Defined in: packages/core/src/contexts/httpcontext.ts:266
Return protocol, hostname and port
Returns
string
the result string
getPathName()
getPathName():
string
Defined in: packages/core/src/contexts/httpcontext.ts:443
Get request path name
Returns
string
the result
See
https://developer.mozilla.org/en-US/docs/Web/API/URL/pathname
getPort()
getPort():
string
Defined in: packages/core/src/contexts/httpcontext.ts:235
Get port number as string
If http on port 80, or https on port 443 will return ""
Returns
string
the result string
getPortNumber()
getPortNumber():
number
Defined in: packages/core/src/contexts/httpcontext.ts:243
Get the port number
Returns
number
the result number
getProtocol()
getProtocol():
"http:"|"https:"
Defined in: packages/core/src/contexts/httpcontext.ts:290
Get protocol used
Returns
"http:" | "https:"
the result
getRawBody()
getRawBody(
limit?,timeout?):Promise<Buffer<ArrayBufferLike>>
Defined in: packages/core/src/contexts/httpcontext.ts:328
Get request body
Parameters
limit?
number = ...
the size of readable request
timeout?
number = 60000
the time to read the request
Returns
Promise<Buffer<ArrayBufferLike>>
the result
Throws
HttpError 413 if the body is bigger than limit, 408 if it is not read within timeout
getRawBodyAsString()
getRawBodyAsString(
limit?,timeout?,encoding?):Promise<string>
Defined in: packages/core/src/contexts/httpcontext.ts:306
Get the raw body as string
Any WHATWG Encoding Standard label is accepted (utf-8, iso-8859-1, shift_jis, gbk, utf-16le, ...) A leading BOM is stripped
Parameters
limit?
number = ...
the size of readable request
timeout?
number = 60000
the time to read the request
encoding?
string
to decode with, default to the Content-Type charset or utf-8
Returns
Promise<string>
the result
Throws
UnsupportedMediaType if the encoding is not supported
getRawStream()
getRawStream():
Readable
Defined in: packages/core/src/contexts/httpcontext.ts:381
Get the body as stream
Returns
Readable
the result
getRelativeUri()
getRelativeUri():
string
Defined in: packages/core/src/contexts/httpcontext.ts:199
Return Uri without prefix
The prefix is only removed on a path boundary, so prefix /prod does not strip /production
Returns
string
the result string
getSearch()
getSearch():
string
Defined in: packages/core/src/contexts/httpcontext.ts:453
Get search section
Returns
string
the result
See
https://developer.mozilla.org/en-US/docs/Web/API/URL/search
getUniqueHeader()
getUniqueHeader(
name,def?):string
Defined in: packages/core/src/contexts/httpcontext.ts:412
Return the last header found with that name
Parameters
name
string
the name to use
def?
string
the default value
Returns
string
the result string
getUrl()
getUrl():
string
Defined in: packages/core/src/contexts/httpcontext.ts:217
Get full URI
Returns
string
the result string
setBody()
setBody(
body):this
Defined in: packages/core/src/contexts/httpcontext.ts:425
Used for test
Parameters
body
any
the request body
Returns
this
this for chaining
setClientCertificate()
setClientCertificate(
cert):this
Defined in: packages/core/src/contexts/httpcontext.ts:158
Set the client certificate presented with the request
Parameters
cert
the certificate, undefined to remove it
Returns
this
this for chaining
setClientIp()
setClientIp(
ip):this
Defined in: packages/core/src/contexts/httpcontext.ts:140
Set the client ip
Parameters
ip
string
the IP address
Returns
this
this for chaining
setPrefix()
setPrefix(
prefix):void
Defined in: packages/core/src/contexts/httpcontext.ts:186
Parameters
prefix
string
uri to not consider
Returns
void