wigm4n opened a new issue, #11549:
URL: https://github.com/apache/apisix/issues/11549
### Description
<h3>ENVIROMENT</h3>
We have the following infrastructure chain: nginx load balancer, then
APISIX, then backend application.
Each part is responsible for its own:
- nginx load balancer is required to publish the API on the Internet;
- APISIX is required to configure rate limiting according to certain rules
and initial validation of the request;
- the backend application provides the main API for the user.
<h3>REQUIREMENT</h3>
In cases where APISIX has to limit the request due to exceeding the request
rate limit, it must return a user-friendly response in json format. For example:
```json
{
"message": "The request limit for your user has been exceeded",
"code": "TOO_MANY_REQUESTS"
}
```
The setting of rate limiting in APISIX must depend on which user is calling
the API. Let's say to recognize which user is calling the API now, he must pass
his ID in a custom header. If this header is missing, the response should
return an understandable response with the response body in json format:
```json
{
"message": "No custom header found",
"code": "BAD_REQUEST"
}
```
<h3>THE CHOSEN SOLUTION</h3>
To configure the rate limiting functionality, the
[limit-count](https://apisix.apache.org/docs/apisix/plugins/limit-count/)
plugin was selected. After studying its documentation and trying it out in
practice, we found out that it is impossible to customize the response body.
[Here](https://github.com/apache/apisix/blob/e74674649fde29cc59ae70be1f8a6f2266b3ba40/apisix/plugins/limit-count/init.lua#L264)
you can see that the response body is only wrapped in the **error_msg** field,
it is impossible to write your own custom response body.
<details>
<summary><h5>limit-count config example</h5></summary>
```yaml
plugins:
limit-count:
count: 6
time_window: 1
rejected_code: 429
rejected_msg: "{\"message\":\"The request limit for your user
has been exceeded\",\"code\":\"TOO_MANY_REQUESTS\"}"
key_type: "var"
key: "http_client_id"
```
</details>
To check the requested url, the
[request-validation](https://apisix.apache.org/docs/apisix/plugins/request-validation/)
plugin was selected. By analogy with the limit-count plugin, here we also did
not find how to customize the response body.
<details>
<summary><h5>request-validation config example</h5></summary>
```yaml
request-validation:
header_schema:
rejected_msg: "{\"message\":\"No custom header
found\",\"code\":\"BAD_REQUEST\"}"
rejected_code: 233
type: object
required: ["Client-ID"]
properties:
"Client-ID":
type: string
pattern: "\\d$"
```
</details>
<h3>FEATURE REQUEST</h3>
So, I would like to be able to customize the response body, specify the
response content-type and the response code in that plugins.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]