You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "wigm4n (via GitHub)" <gi...@apache.org> on 2024/09/03 15:29:00 UTC

[I] feat: As a user, I want to customize response body, so that i can use apisix as request preprocessing [apisix]

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: notifications-unsubscribe@apisix.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] feat: As a user, I want to customize response body, so that i can use APISIX as request preprocessing [apisix]

Posted by "zhoujiexiong (via GitHub)" <gi...@apache.org>.
zhoujiexiong commented on issue #11549:
URL: https://github.com/apache/apisix/issues/11549#issuecomment-2328043369

   @wigm4n 
   
   What about using [serverless plugin](https://apisix.apache.org/zh/docs/apisix/plugins/serverless/) to write some LUA? :D


-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] feat: As a user, I want to customize response body, so that i can use APISIX as request preprocessing [apisix]

Posted by "zhoujiexiong (via GitHub)" <gi...@apache.org>.
zhoujiexiong commented on issue #11549:
URL: https://github.com/apache/apisix/issues/11549#issuecomment-2328085567

   > > @wigm4n
   > > What about using [serverless plugin](https://apisix.apache.org/zh/docs/apisix/plugins/serverless/) to write some LUA? :D
   > 
   > I've already thought this way... But not only my knowledge of LUA is small, but also our admins, that this can lead to bugs and unexpected consequences
   > 
   > When I read about APISIX, it is described everywhere as a very flexible tool, but I cannot even change the response body...
   
   There may be other options:
   1. request-validation + response-rewrite plugins
   2. ext-plugin


-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] feat: As a user, I want to customize response body, so that i can use APISIX as request preprocessing [apisix]

Posted by "wigm4n (via GitHub)" <gi...@apache.org>.
wigm4n commented on issue #11549:
URL: https://github.com/apache/apisix/issues/11549#issuecomment-2328056373

   > @wigm4n
   > 
   > What about using [serverless plugin](https://apisix.apache.org/zh/docs/apisix/plugins/serverless/) to write some LUA? :D
   
   I've already thought this way... But not only my knowledge of LUA is small, but also our admins, that this can lead to bugs and unexpected consequences


-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] feat: As a user, I want to customize response body, so that i can use APISIX as request preprocessing [apisix]

Posted by "moonming (via GitHub)" <gi...@apache.org>.
moonming closed issue #11549: feat: As a user, I want to customize response body, so that i can use APISIX as request preprocessing
URL: https://github.com/apache/apisix/issues/11549


-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org