Custom response

Description

custom-response plugin implements a function of sending custom responses, including custom HTTP response status codes, HTTP response headers and HTTP response body, which can be used in the scenarios of response mocking and sending a custom response for specific status codes, such as customizing the response for rate-limited requests.

Configuration Fields

NameTypeRequirementDefault ValueDescription
status_codenumberOptional200Custom HTTP response status code
headersarray of stringOptional-Custom HTTP response header. Key and value shall be separated using =.
bodystringOptional-Custom HTTP response body
enable_on_statusarray of numberOptional-The original response status code to match. Generate the custom response only the actual status code matches the configuration. Ignore the status code match if left unconfigured.

Configuration Samples

Mock Responses

  1. status_code: 200
    headers:
    - Content-Type=application/json
    - Hello=World
    body: “{\”hello\”:\”world\”}”

According to the configuration above, all the requests will get the following custom response:

  1. HTTP/1.1 200 OK
    Content-Type: application/json
    Hello: World
    Content-Length: 17
  2. {“hello”:”world”}

Send a Custom Response when Rate-Limited

  1. enable_on_status:
    - 429
    status_code: 302
    headers:

When rate-limited, normally gateway will return a status code of 429 . Now, rate-limited requests will get the following custom response:

  1. HTTP/1.1 302 Found

So based on the 302 redirecting mechanism provided by browsers, this can redirect rate-limited users to other pages, for example, a static page hosted on CDN.

If you’d like to send other responses when rate-limited, please add other fields into the configuration, referring to the Mock Responses scenario.

Only Enabled for Specific Routes or Domains

  1. # Use rules field for fine-grained rule configurations
    rules:
    # Rule 1: Match by route name
    - match_route:
    - route-a
    - route-b
    body: “{\”hello\”:\”world\”}”
    # Rule 2: Match by domain
    - match_domain:
    - “*.example.com”
    - test.com
    enable_on_status:
    - 429
    status_code: 200
    headers:
    - Content-Type=application/json
    body: “{\”errmsg\”: \”rate limited\”}”

In the rule sample of _match_route_, route-a and route-b are the route names provided when creating a new gateway route. When the current route names matches the configuration, the rule following shall be applied. In the rule sample of _match_domain_, *.example.com and test.com are the domain names used for request matching. When the current domain name matches the configuration, the rule following shall be applied. All rules shall be checked following the order of items in the _rules_ field, The first matched rule will be applied. All remained will be ignored.