qwzhou89 commented on issue #9856:
URL: https://github.com/apache/apisix/issues/9856#issuecomment-1645498179

   Using the following code, which avoids the use of 
core.schema.check(conf.body_schema, req_body), achieves what I need
   ```yaml
   uri: /test-send-raw-split
   name: test-send-raw-split
   plugins:
     proxy-rewrite:
       uri: /?check=$check_result
     serverless-pre-function:
       functions:
         - |
           local core = require('apisix.core');
           local jsonschema   = require("jsonschema")
           local schema = {
             type = 'object',
             required = {'method'},
             properties = {
               method = {
                 pattern = '^(?!aaa|bbb)',
                 type = 'string'
               }
             }
           }
           local validator = jsonschema.generate_validator(schema)
   
           return function(conf, ctx)
             core.ctx.register_var("check_result", function(ctx)
               local result = "ok"
               local req_body
               local body, err = core.request.get_body()
               if not body then
                 if err then
                   core.log.error("failed to get body: ", err)
                 end
                 result = "no"
               end
   
               local headers = core.request.headers(ctx)
               if headers["content-type"] == 
"application/x-www-form-urlencoded" then
                 -- use 0 to avoid truncated result and keep the behavior as the
                 -- same as other platforms
                 req_body, err = ngx.decode_args(body, 0)
               else -- JSON as default
                 req_body, err = core.json.decode(body)
               end
   
               if not req_body then
                 core.log.error('failed to decode the req body: ', err)
                 result = "no"
               end
   
               local ok, err = validator(req_body)
               if not ok then
                 core.log.error("req schema validation failed: ", err)
                 result = "no"
               end
               return result
             end)
           end
       phase: rewrite
     traffic-split:
       rules:
         - match:
             - vars:
                 - - check_result
                   - '=='
                   - ok
           weighted_upstreams:
             - upstream:
                 nodes:
                   echo-api.3scale.net:80: 1
                 type: roundrobin
         - match:
             - vars:
                 - - check_result
                   - '=='
                   - 'no'
           weighted_upstreams:
             - upstream:
                 nodes:
                   echo-api.3scale.net:80: 1
                 type: roundrobin
   status: 1
   
   ``` 


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