Skip to main content

HttpContext

@webda/core


Class: HttpContext

Defined in: packages/core/src/contexts/httpcontext.ts:25

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:67

Create a new HttpContext

Parameters​

hostname​

string

the request hostname

method​

HttpMethodType

the HTTP method

uri​

string

the request URI

protocol?​

"http" | "https"

the protocol

port?​

string | number

the port number

headers?​

the request headers

Returns​

HttpContext

Properties​

body​

body: Buffer<ArrayBufferLike> | Readable

Defined in: packages/core/src/contexts/httpcontext.ts:51


clientIp​

clientIp: string

Defined in: packages/core/src/contexts/httpcontext.ts:43

Get the client ip if available


cookies​

cookies: any

Defined in: packages/core/src/contexts/httpcontext.ts:52


headers​

headers: HeadersRequest

Defined in: packages/core/src/contexts/httpcontext.ts:48


host​

host: string

Defined in: packages/core/src/contexts/httpcontext.ts:50


hostname​

hostname: string

Defined in: packages/core/src/contexts/httpcontext.ts:31

Hostname

Example​

test.webda.io

method​

method: HttpMethodType

Defined in: packages/core/src/contexts/httpcontext.ts:35

HTTP Method


origin​

origin: string

Defined in: packages/core/src/contexts/httpcontext.ts:49


path​

path: string

Defined in: packages/core/src/contexts/httpcontext.ts:44


port​

port: string

Defined in: packages/core/src/contexts/httpcontext.ts:47


prefix​

prefix: string = ""

Defined in: packages/core/src/contexts/httpcontext.ts:57

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:46


search: string

Defined in: packages/core/src/contexts/httpcontext.ts:45


uri​

uri: string

Defined in: packages/core/src/contexts/httpcontext.ts:39

Pathname

Methods​

getAbsoluteUrl()​

getAbsoluteUrl(uri?): string

Defined in: packages/core/src/contexts/httpcontext.ts:397

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


getClientIp()​

getClientIp(): string

Defined in: packages/core/src/contexts/httpcontext.ts:123

Get the client ip

Returns​

string

the result string


getCookies()​

getCookies(): any

Defined in: packages/core/src/contexts/httpcontext.ts:179

Get cookies

Returns​

any

the result


getHeader()​

getHeader(name, def?): string | string[]

Defined in: packages/core/src/contexts/httpcontext.ts:334

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:324

Get HTTP Headers

Returns​

Readonly<IncomingHttpHeaders>

the result


getHost()​

getHost(): string

Defined in: packages/core/src/contexts/httpcontext.ts:212

Return hostname and port

Returns​

string

the result string


getHostName()​

getHostName(): string

Defined in: packages/core/src/contexts/httpcontext.ts:228

Get the hostname

Returns​

string

the result string


getHref()​

getHref(): string

Defined in: packages/core/src/contexts/httpcontext.ts:132

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:236

Get HTTP Method used

Returns​

HttpMethodType

the result


getOrigin()​

getOrigin(): string

Defined in: packages/core/src/contexts/httpcontext.ts:220

Return protocol, hostname and port

Returns​

string

the result string


getPathName()​

getPathName(): string

Defined in: packages/core/src/contexts/httpcontext.ts:375

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:189

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:197

Get the port number

Returns​

number

the result number


getProtocol()​

getProtocol(): "http:" | "https:"

Defined in: packages/core/src/contexts/httpcontext.ts:244

Get protocol used

Returns​

"http:" | "https:"

the result


getRawBody()​

getRawBody(limit?, timeout?): Promise<Buffer<ArrayBufferLike>>

Defined in: packages/core/src/contexts/httpcontext.ts:279

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


getRawBodyAsString()​

getRawBodyAsString(limit?, timeout?, encoding?): Promise<string>

Defined in: packages/core/src/contexts/httpcontext.ts:256

Get the raw body as string

Parameters​

limit?​

number = ...

the size of readable request

timeout?​

number = 60000

the time to read the request

encoding?​

string

to analyze

Returns​

Promise<string>

the result


getRawStream()​

getRawStream(): Readable

Defined in: packages/core/src/contexts/httpcontext.ts:313

Get the body as stream

Returns​

Readable

the result


getRelativeUri()​

getRelativeUri(): string

Defined in: packages/core/src/contexts/httpcontext.ts:153

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:385

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:344

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:171

Get full URI

Returns​

string

the result string


setBody()​

setBody(body): this

Defined in: packages/core/src/contexts/httpcontext.ts:357

Used for test

Parameters​

body​

any

the request body

Returns​

this

this for chaining


setClientIp()​

setClientIp(ip): this

Defined in: packages/core/src/contexts/httpcontext.ts:114

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:140

Parameters​

prefix​

string

uri to not consider

Returns​

void