我是靠谱客的博主 兴奋绿茶,最近开发中收集的这篇文章主要介绍apisix插件之修改返回body,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1、修改请求返回body的插件,直接上代码:

local core = require("apisix.core")
local ngx = ngx
local string = string
--插件配置json语法
local schema = {
    properties = {
        before_body = {
            description = "body before the filter phase.",
            type = "string"
        },
        body = {
            description = "body to replace upstream response.",
            type = "string"
        },
        after_body = {
            description = "body after the modification of filter phase.",
            type = "string"
        }
    },
    anyOf = {
        {required = {"before_body"}},
        {required = {"body"}},
        {required = {"after_body"}}
    },
    minProperties = 1
}
--插件名称
local plugin_name = "eag"

local _M = {
    version = 0.1,
    priority = 20004, --插件优先级
    name = plugin_name,
    schema = schema,
}
--插件配置检验
function _M.check_schema(conf)
    return core.schema.check(schema, conf)
end
--返回值body修改同时必须修改ngx.header.content_length
function _M.body_filter(conf, ctx)
    local request_uri = ngx.var.uri  

    if conf.after_body then
        ngx.arg[1] = string.upper(conf.body)
    end

    ngx.arg[2] = true
end
--修改ngx.header.content_length
function _M.header_filter(conf, ctx)
    if conf.body then
        ngx.header.content_length = #conf.body
        -- in case of upstream content is compressed content
        ngx.header.content_encoding = nil
    end
end

 

最后

以上就是兴奋绿茶为你收集整理的apisix插件之修改返回body的全部内容,希望文章能够帮你解决apisix插件之修改返回body所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部