apisix icon indicating copy to clipboard operation
apisix copied to clipboard

request help: Advanced routing settings

Open aijunziaixifu opened this issue 6 years ago • 4 comments

Issue description

Environment

  • apisix-version:2.0
  • OS:centos 7

请问一下,高级路由配置的请求参数选项,其参数值是从body获取还是,路径获取,是否有解析body的插件呢 image

aijunziaixifu avatar Nov 17 '20 08:11 aijunziaixifu

@aijunziaixifu You can read about var in here

idbeta avatar Nov 17 '20 08:11 idbeta

@aijunziaixifu please use English in public channel

membphis avatar Nov 17 '20 16:11 membphis

Hi, @aijunziaixifu, As the @spacewander answered in https://github.com/apache/apisix/issues/2763, you can write your filter function.

Here, I give you an example, you can have a try.

curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '{
    "uri": "/index.html",
    "methods": ["PUT", "GET"],
    "upstream": {
        "type": "roundrobin",
        "nodes": {
            "39.97.63.215:80": 1
        }
    },
    "filter_func": "function(vars)
    local core = require (\"apisix.core\")
    local body, err = core.request.get_body()
    if not body then
        return false
    end

    local data, err = core.json.decode(body)
    if not data then
        return false
    end

    if data[\"a\"] == \"b\" then
        return true
    end

    return false
end"
}'

By the way, I had seen many people have questions about this. May I add this to the document?

starsz avatar Nov 18 '20 02:11 starsz

Hi, @aijunziaixifu, As the @spacewander answered in #2763, you can write your filter function.

Here, I give you an example, you can have a try.

curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '{
    "uri": "/index.html",
    "methods": ["PUT", "GET"],
    "upstream": {
        "type": "roundrobin",
        "nodes": {
            "39.97.63.215:80": 1
        }
    },
    "filter_func": "function(vars)
    local core = require (\"apisix.core\")
    local body, err = core.request.get_body()
    if not body then
        return false
    end

    local data, err = core.json.decode(body)
    if not data then
        return false
    end

    if data[\"a\"] == \"b\" then
        return true
    end

    return false
end"
}'

By the way, I had seen many people have questions about this. May I add this to the document?

That can be classified as one section of "Apache APISIX Best Practices".

tokers avatar Nov 18 '20 03:11 tokers