HttpContext
Class: HttpContext
Defined in: packages/core/src/contexts/httpcontext.ts:53
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:126
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:103
Body
clientCertificate?
optionalclientCertificate?:ClientCertificate
Defined in: packages/core/src/contexts/httpcontext.ts:111
Client certificate presented with the request (mTLS), if any
clientIp
clientIp:
string
Defined in: packages/core/src/contexts/httpcontext.ts:71
Get the client ip if available
cookies
cookies:
any
Defined in: packages/core/src/contexts/httpcontext.ts:107
Cookies
headers
headers:
HeadersRequest
Defined in: packages/core/src/contexts/httpcontext.ts:91
Headers
host
host:
string
Defined in: packages/core/src/contexts/httpcontext.ts:99
Host
hostname
hostname:
string
Defined in: packages/core/src/contexts/httpcontext.ts:59
Hostname
Example
test.webda.io
method
method:
HttpMethodType
Defined in: packages/core/src/contexts/httpcontext.ts:63
HTTP Method
origin
origin:
string
Defined in: packages/core/src/contexts/httpcontext.ts:95
Origin
path
path:
string
Defined in: packages/core/src/contexts/httpcontext.ts:75
Path
port
port:
string
Defined in: packages/core/src/contexts/httpcontext.ts:87
Port
prefix
prefix:
string=""
Defined in: packages/core/src/contexts/httpcontext.ts:116
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:83
Protocol
search
search:
string
Defined in: packages/core/src/contexts/httpcontext.ts:79
Search
uri
uri:
string
Defined in: packages/core/src/contexts/httpcontext.ts:67
Pathname
Methods
getAbsoluteUrl()
getAbsoluteUrl(
uri?):string
Defined in: packages/core/src/contexts/httpcontext.ts:498
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:202
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:182
Get the client ip
Returns
string
the result string
getCookies()
getCookies():
any
Defined in: packages/core/src/contexts/httpcontext.ts:258
Get cookies
Returns
any
the result
getHeader()
getHeader(
name,def?):string|string[]
Defined in: packages/core/src/contexts/httpcontext.ts:435
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:425
Get HTTP Headers
Returns
Readonly<IncomingHttpHeaders>
the result
getHost()
getHost():
string
Defined in: packages/core/src/contexts/httpcontext.ts:291
Return hostname and port
Returns
string
the result string
getHostName()
getHostName():
string
Defined in: packages/core/src/contexts/httpcontext.ts:307
Get the hostname
Returns
string
the result string
getHref()
getHref():
string
Defined in: packages/core/src/contexts/httpcontext.ts:211
Get the href
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:315
Get HTTP Method used
Returns
the result
getOrigin()
getOrigin():
string
Defined in: packages/core/src/contexts/httpcontext.ts:299
Return protocol, hostname and port
Returns
string
the result string
getPathName()
getPathName():
string
Defined in: packages/core/src/contexts/httpcontext.ts:476
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:268
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:276
Get the port number
Returns
number
the result number
getProtocol()
getProtocol():
"http:"|"https:"
Defined in: packages/core/src/contexts/httpcontext.ts:323
Get protocol used
Returns
"http:" | "https:"
the result
getRawBody()
getRawBody(
limit?,timeout?):Promise<Buffer<ArrayBufferLike>>
Defined in: packages/core/src/contexts/httpcontext.ts:361
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:339
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:414
Get the body as stream
Returns
Readable
the result
getRelativeUri()
getRelativeUri():
string
Defined in: packages/core/src/contexts/httpcontext.ts:232
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:486
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:445
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:250
Get full URI
Returns
string
the result string
setBody()
setBody(
body):this
Defined in: packages/core/src/contexts/httpcontext.ts:458
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:191
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:173
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:219
Set the prefix
Parameters
prefix
string
uri to not consider
Returns
void