mikyll commented on issue #11801:
URL: https://github.com/apache/apisix/issues/11801#issuecomment-2622295200

   Modulo operator is supported via `filter_func` parameter 🙂 
   
   ### Example
   
   A route that matches only if the value of a query parameter 
`test_query_param` is even.
   
   Route configuration (admin API):
   
   ```bash
   curl http://127.0.0.1:9180/apisix/admin/routes/filterfunc_modulo -H 
"X-API-KEY: $admin_key" -X PUT -i -d '
   {
     "uri": "/filter_func_modulo*",
     "filter_func": "function(vars); return tonumber(vars.arg_test_query_param) 
% 2 == 0; end",
     "upstream": {
         "type": "roundrobin",
         "nodes": {
             "httpbin.org": 1
         }
     },
     "plugins": {
       "proxy-rewrite": {
         "uri": "/get"
       }
     }
   }'
   ```
   
   Test the route:
   
   - This fails because 1 is odd (`1 % 2 != 0`)
   
       ```bash
       curl localhost:9080/filter_func_modulo?test_query_param=1
       {"error_msg":"404 Route Not Found"}
       ```
   
   - This works because 1 is even (`1 % 2 == 0`)
     
       ```bash
       curl localhost:9080/filter_func_modulo?test_query_param=2
       {
         "args": {
           "test_query_param": "2"
         }, 
         "headers": {
           "Accept": "*/*", 
           "Host": "localhost", 
           "User-Agent": "curl/8.5.0", 
           "X-Amzn-Trace-Id": "Root=1-679a6273-1640eed1461de3267938d3ad", 
           "X-Forwarded-Host": "localhost"
         }, 
         "origin": "172.19.0.1, X.X.X.X", 
         "url": "http://localhost/get?test_query_param=2";
       }
       ```
   
   The example can be extended to also sanitize/validate the parameter.


-- 
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]

Reply via email to