解决openresty http客户端不支持https的问题

 OpenResty默认没有提供Http客户端,须要使用第三方提供;固然咱们能够经过ngx.location.capture 去方式实现,但它只能发送一个子请求。html

第三方基本是以lua-resty-http为表明,这个类库若是去访问http和正规的https是没有问题,也挺好用的,但若是访问使用山寨证书的请求会出一些错误,好比:handshake failed,socket error等等之类的错误。对于种个人解决办法是使用curl,能够很好解决这个问题,如今来看算是比较完美的。
具体代码以下:
json

 

local curl = require("luacurl")

local function postJson(url,postData,c)
    local result = { }
    if c == nil then
        c = curl.new()
    end
    c:setopt(curl.OPT_URL, url)
    c:setopt(curl.OPT_SSL_VERIFYHOST,0)
    c:setopt(curl.OPT_SSL_VERIFYPEER,false)
    c:setopt(curl.OPT_POST,true)
    c:setopt(curl.OPT_HTTPHEADER, "Content-Type: application/json")
    c:setopt(curl.OPT_POSTFIELDS, postData)
    c:setopt(curl.OPT_WRITEDATA, result)
    c:setopt(curl.OPT_WRITEFUNCTION, function(tab, buffer)
        table.insert(tab, buffer)
        return #buffer
    end)
    local ok = c:perform()
    return ok, table.concat(result)
end

 local ok,html = postJson(serverUrl,data);
        if ok then
            ngx.say(html)
        end
相关文章
相关标签/搜索