Request Context

Request Context #

What Is Context #

Context is the entry for INFINI Gateway to access relevant information in the current running environment, such as the request source and configuration. You can use the _ctx keyword to access relevant fields, for example, _ctx.request.uri, which indicates the requested URL.

Embedded Request Context #

The embedded _ctx context objects of an HTTP request mainly include the following:

NameTypeDescription
iduint64Unique ID of the request
tlsboolWhether the request is a TLS request
remote_ipstringSource IP of the client
remote_addrstringSource IP address of the client, including port
local_ipstringGateway local IP address
local_addrstringGateway local IP address, including port
elapsedint64Time that the request has been executed (ms)
request.*objectRequest description
response.*objectResponse description

request #

The request object has the following attributes:

NameTypeDescription
to_stringstringComplete HTTP request in text form
hoststringAccessed destination host name/domain name
methodstringRequest type
uristringComplete URL of request
pathstringRequest path
query_argsmapURL request parameter
usernamestringName of the user who initiates the request
passwordstringPassword of the user
headermapHeader parameter
bodystringRequest body
body_jsonobjectJSON request body object
body_lengthintRequest body length

If the request body data submitted by the client is in JSON format, you can use body_json to access the data. See the following example.

curl -u tesla:password -XGET "http://localhost:8000/medcl/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query":{
	"bool":{
	"must":[{"match":{"name":"A"}},{"match":{"age":18}}]
	}
},
"size":900,
  "aggs": {
    "total_num": {
      "terms": {
        "field": "name1",
        "size": 1000000
      }
    }
  }
}'

In JSON data, . is used to identify the path. If the data is an array, you can use [Subscript] to access a specified element, for example, you can use a dump filter for debugging as follows:

  - name: cache_first
    filter:
      - dump:
          context:
            - _ctx.request.body_json.size
            - _ctx.request.body_json.aggs.total_num.terms.field
            - _ctx.request.body_json.query.bool.must.[1].match.age

The output is as follows:

_ctx.request.body_json.size  :  900
_ctx.request.body_json.aggs.total_num.terms.field  :  name1
_ctx.request.body_json.query.bool.must.[1].match.age  :  18

response #

The response object has the following attributes:

NameTypeDescription
to_stringstringComplete HTTP response in text form
statusintRequest status code
headermapHeader parameter
content_typestringResponse body type
bodystringResponse body
body_jsonobjectJSON request body object
body_lengthintResponse body length

System Context #

The _sys.* object has the following attributes:

NameTypeDescription
hostnamestringHostname of the gateway deployed
month_of_nowintMonth of now, range from [1,12]
weekday_of_nowintWeekday of now, range from [0,6], 0 is Sunday
day_of_nowintDay of now
hour_of_nowintHour of now, range from [0,23]
minute_of_nowintMinute of now, range from [0,59]
second_of_nowintSecond of now, range from [0,59]
unix_timestamp_of_nowint64Unix timestamp of the current time
unix_timestamp_milli_of_nowint64Unix timestamp of the current time in milliseconds

Utility Context #

These are utility context can ube used for quickly obtaining relevant parameters. The object _util.* has the following properties:

NameTypeDescription
generate_uuidstringRetrieve a random UUID string parameter
increment_idstringRetrieve an auto-incrementing numeric identifier. Default bucket is default, customize bucket are supported, e.g., _util.increment_id.mybucket
Edit Edit this page