apisix 自定义lua插件 webservice转restful 解析返回xml数据为json字符串

apisix.tree:https://download.csdn.net/download/FSWZYC/88541181
apisix.xml2lua:https://download.csdn.net/download/FSWZYC/88541178

--
-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements.  See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You under the Apache License, Version 2.0
-- (the "License"); you may not use this file except in compliance with
-- the License.  You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
local core = require("apisix.core")
local http = require("resty.http")
local xml2lua = require("apisix.xml2lua")
local handler = require("apisix.tree")

local plugin_name = "webservice-proxy"

local schema = {
    type = "object",
    properties = {
        namespace = {
            type = "string",
            minLength = 1,
        },
        method_name = {
            type = "string",
            minLength = 1,
        },
        url = {
            type = "string",
            minLength = 1,
        },
    },
    required = { "namespace", "method_name", "url"},
}

local _M = {
    version = 0.1,
    priority = 9999,
    name = plugin_name,
    schema = schema,
}

function _M.check_schema(conf)
    return core.schema.check(schema, conf)
end



function _M.access(conf, ctx)
    ngx.req.read_body()
    --core.log.warn(ngx.req.get_body_data())
    local bodyData = ngx.req.get_body_data();
    local parameter = ""
    if bodyData ~= nil then
        for k, v in pairs(core.json.decode(ngx.req.get_body_data())) do
            parameter = parameter .. "<web:" .. k .. ">" .. v .. "</web:" .. k .. ">\n"
        end
    end
    -- 示例调用
    local method = "POST"
    local headers = {
        ["Content-Type"] = "text/xml;charset=utf-8",
        -- 添加其他必要的请求头
    }
    local body = [[
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="%s">
           <soapenv:Header/>
           <soapenv:Body>
              <web:%s>
                 %s
              </web:%s>
           </soapenv:Body>
        </soapenv:Envelope>
    ]]

    local request_xml = string.format(body, conf.namespace, conf.method_name, parameter, conf.method_name)
    local response, err = Call_webservice(conf.url, method, headers, request_xml)
    if err then
        core.log.warn("failed to call webservice: ", err)
        -- 处理错误情况
        return 500, err;
    else
        -- 处理正常情况
        local hadle_new = handler:new()
        local parser = xml2lua.parser(hadle_new)
        parser:parse(response)
        local myTable = hadle_new.root
        local envelope = myTable["Envelope"]["Body"];

        --handler.root = nil
        return 200, core.json.encode(envelope);
    end

end

function Call_webservice(url, method, headers, body)
    -- 创建 HTTP 客户端
    local httpc = http.new()

    -- 发起请求
    local res, err = httpc:request_uri(url, {
        method = method,
        headers = headers,
        body = body
    })

    -- 检查请求是否成功
    if not res then
        core.log.err("failed to request: ", err)
        return nil, err
    end

    -- 返回响应
    return res.body, nil
end

return _M

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值