Created
May 5, 2022 07:21
-
-
Save badri/3253c3e0c60d713b5ed5f73a2b8d382d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local base64 = require "ngx.base64" | |
local httpc = require("resty.http").new() | |
local ngx_print = ngx.print | |
local base64_encode = base64.encode_base64url | |
local proxy_opts = {} | |
-- remove below 2 lines if not using proxy auth | |
local auth_header = "Basic " .. base64_encode("holosix:xN186VaYjrCGepJI") | |
proxy_opts.http_proxy_authorization = auth_header | |
-- substitute with your proxy address and port | |
proxy_opts.http_proxy = "http://squid.kong-dp.svc.cluster.local:3128" | |
local ok, err = httpc:connect { | |
scheme = "http", | |
host = "httpbin.kong-dp.svc.cluster.local", -- the service you want to call | |
port = 8000, | |
proxy_opts = proxy_opts, | |
ssl_verify = false, | |
} | |
if not ok then | |
ngx.log(ngx.ERR, "failed to connect to proxy: ", err) | |
return | |
end | |
local res, err = httpc:request({ | |
method = "GET", | |
path = "/uuid", -- the path of the service | |
}) | |
if not res then | |
ngx.log(ngx.ERR, "request failed: ", err) | |
return | |
end | |
local status = res.status | |
local length = res.headers["Content-Length"] | |
local reader = res.body_reader | |
repeat | |
local chunk, ok, read_err, print_err | |
chunk, read_err = reader() | |
if read_err then | |
ngx.log(ngx.ERR, read_err) | |
end | |
if chunk then | |
ok, print_err = ngx_print(chunk) | |
if not ok then | |
ngx.log(ngx.ERR, print_err) | |
end | |
end | |
ngx.log(ngx.ERR,"body:", chunk) | |
if read_err or print_err then | |
break | |
end | |
until not chunk | |
ngx.log(ngx.ERR, "length: ", length) | |
ngx.log(ngx.ERR, "status: ", status) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment