Sn0rt commented on issue #9856:
URL: https://github.com/apache/apisix/issues/9856#issuecomment-1641716375
# reproduce step
create a file `router.json`
```json
{
"uri": "/test-send-raw-split",
"name": "test-send-raw-split",
"plugins": {
"serverless-pre-function": {
"functions": ["local core = require('apisix.core');\nlocal conf = {\n
check_result = 'ok',\n body_schema = {\n properties = {\n method = {\n
pattern = '^(?!aaa|bbb)',\n type = 'string'\n }\n },\n
required = {'method'},\n type = 'object'\n }\n}\n\nreturn function(conf,
ctx)\n core.ctx.register_var(\"check_result\", function(ctx)\n local
req_body\n local body, err = core.request.get_body()\n if not body then\n
if err then\n core.log.error(\"failed to get body: \", err)\n
end\n conf.check_result = \"no\"\n end\n\n local headers =
core.request.headers(ctx)\n if headers[\"content-type\"] ==
\"application/x-www-form-urlencoded\" then\n -- use 0 to avoid truncated
result and keep the behavior as the\n -- same as other platforms\n
req_body, err = ngx.decode_args(body, 0)\n else -- JSON as default\n
req_body, err = core.json.decode(body)\n end\n\n if not req_body then\n
c
ore.log.error('failed to decode the req body: ', err)\n conf.check_result
= \"no\"\n end\n\n local ok, err = core.schema.check(conf.body_schema,
req_body)\n if not ok then\n core.log.error(\"req schema validation
failed: \", err)\n conf.check_result = \"no\"\n end\n\n return
conf.check_result\n end)\nend\n"],
"phase": "rewrite"
},
"traffic-split": {
"rules": [{
"match": [{
"vars": [
["check_result", "==", "ok"]
]
}],
"weighted_upstreams": [{
"upstream": {
"nodes": {
"echo-api.3scale.net": 1
},
"type": "roundrobin"
}
}]
}, {
"match": [{
"vars": [
["check_result", "==", "no"]
]
}],
"weighted_upstreams": [{
"upstream": {
"nodes": {
"echo-api.3scale.net": 1
},
"type": "roundrobin"
}
}]
}]
}
},
"status": 1
}
```
create a router
```shell
bash-5.1# curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1' -X PUT -d @router.json
{"node":{"key":"\/apisix\/routes\/1","value":{"id":"1","plugins":{"serverless-pre-function":{"phase":"rewrite","functions":["local
core = require('apisix.core');\nlocal conf = {\n check_result = 'ok',\n
body_schema = {\n properties = {\n method = {\n pattern =
'^(?!aaa|bbb)',\n type = 'string'\n }\n },\n required =
{'method'},\n type = 'object'\n }\n}\n\nreturn function(conf, ctx)\n
core.ctx.register_var(\"check_result\", function(ctx)\n local req_body\n
local body, err = core.request.get_body()\n if not body then\n if err
then\n core.log.error(\"failed to get body: \", err)\n end\n
conf.check_result = \"no\"\n end\n\n local headers =
core.request.headers(ctx)\n if headers[\"content-type\"] ==
\"application\/x-www-form-urlencoded\" then\n -- use 0 to avoid truncated
result and keep the behavior as the\n -- same as other platforms\n
req_body, err = ngx.decode_args(body, 0)\n else -- JS
ON as default\n req_body, err = core.json.decode(body)\n end\n\n if
not req_body then\n core.log.error('failed to decode the req body: ',
err)\n conf.check_result = \"no\"\n end\n\n local ok, err =
core.schema.check(conf.body_schema, req_body)\n if not ok then\n
core.log.error(\"req schema validation failed: \", err)\n
conf.check_result = \"no\"\n end\n\n return conf.check_result\n
end)\nend\n"]},"traffic-split":{"rules":[{"weighted_upstreams":[{"upstream":{"hash_on":"vars","pass_host":"pass","type":"roundrobin","nodes":{"echo-api.3scale.net":1},"scheme":"http"},"weight":1}],"match":[{"vars":[["check_result","==","ok"]]}]},{"weighted_upstreams":[{"upstream":{"hash_on":"vars","pass_host":"pass","type":"roundrobin","nodes":{"echo-api.3scale.net":1},"scheme":"http"},"weight":1}],"match":[{"vars":[["check_result","==","no"]]}]}]}},"status":1,"update_time":1689757643,"name":"test-send-raw-split","priority":0,"create_time":1689133203,"uri":"\
/test-send-raw-split"}},"action":"set"}
```
test the API
```shell
bash-5.1# curl -v -H "Content-Type: application/json" --data
'{"jsonrpc":"2.0","method":"cccc","params":[],"id":83}'
http://127.0.0.1:9080/test-send-raw-split
* Trying 127.0.0.1:9080...
* Connected to 127.0.0.1 (127.0.0.1) port 9080 (#0)
> POST /test-send-raw-split HTTP/1.1
> Host: 127.0.0.1:9080
> User-Agent: curl/7.79.1
> Accept: */*
> Content-Type: application/json
> Content-Length: 53
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 500 Internal Server Error
< Date: Wed, 19 Jul 2023 09:08:29 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Connection: close
< Server: APISIX/2.15.0
<
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<title>500 Internal Server Error</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>An error occurred.</h1>
<p>You can report issue to <a
href="https://github.com/apache/apisix/issues">APISIX</a></p>
<p><em>Faithfully yours, <a
href="https://apisix.apache.org/">APISIX</a>.</em></p>
</body>
</html>
* Closing connection 0
```
--
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]