lua-resty-http2
lua-resty-http2 copied to clipboard
发送http2请求时,request close
参照文档中给出的例子,我改造了了一下,如下,并进行访问,得到了 client:request() failed: closed
的错误。
local on_headers_reach = function(ctx, headers)
print("received HEADERS frame:")
for k, v in pairs(headers) do
print(k, ": ", v)
end
end
local on_data_reach = function(ctx, data)
print("received DATA frame:")
print(data)
end
local on_trailers_reach = function(ctx, data)
print("received HEADERS frame for trailer headers:")
for k, v in pairs(headers) do
print(k, ": ", v)
end
end
local host = "10.148.165.39"
local port = 8081
local sock = ngx.socket.tcp()
print('start create socket ... ')
local ok, err = sock:connect(host, port)
if not ok then
print("connect create failed: " .. err)
os.exit(1)
end
print('start create header ... ')
local headers = {
{ name = ":method", value = "GET" },
{ name = ":path", value = "/authorization" },
{ name = ":scheme", value = "http" },
{ name = "accept-encoding", value = "gzip" },
}
print('start create opt ... ')
local opts = {
ctx = sock,
recv = sock.receive,
send = sock.send,
preread_size = 1024,
max_concurrent_stream = 100,
}
print('start create http2 ... ')
local client, err = http2.new(opts)
if not client then
print("client create failed: ", err)
os.exit(2)
end
print('start request ... ')
local ok, err = client:request(headers, nil, on_headers_reach, on_data_reach, on_trailers_reach)
if not ok then
print("client:request() failed: ", err)
os.exit(3)
end
sock:close()
还有两个问题:
- 如果有 query 参数,是直接按规范写在 path 中吗?类似:
local headers = {
{ name = ":method", value = "GET" },
{ name = ":path", value = "/authorization?a=1&b=2&c=3" },
{ name = ":scheme", value = "http" },
{ name = "accept-encoding", value = "gzip" },
}
- 如何获取到返回值呢?如果靠回调函数的话,是否要设置一个全局变量去接收返回值?
参照文档中给出的例子,我改造了了一下,如下,并进行访问,得到了
client:request() failed: closed
的错误。local on_headers_reach = function(ctx, headers) print("received HEADERS frame:") for k, v in pairs(headers) do print(k, ": ", v) end end local on_data_reach = function(ctx, data) print("received DATA frame:") print(data) end local on_trailers_reach = function(ctx, data) print("received HEADERS frame for trailer headers:") for k, v in pairs(headers) do print(k, ": ", v) end end local host = "10.148.165.39" local port = 8081 local sock = ngx.socket.tcp() print('start create socket ... ') local ok, err = sock:connect(host, port) if not ok then print("connect create failed: " .. err) os.exit(1) end print('start create header ... ') local headers = { { name = ":method", value = "GET" }, { name = ":path", value = "/authorization" }, { name = ":scheme", value = "http" }, { name = "accept-encoding", value = "gzip" }, } print('start create opt ... ') local opts = { ctx = sock, recv = sock.receive, send = sock.send, preread_size = 1024, max_concurrent_stream = 100, } print('start create http2 ... ') local client, err = http2.new(opts) if not client then print("client create failed: ", err) os.exit(2) end print('start request ... ') local ok, err = client:request(headers, nil, on_headers_reach, on_data_reach, on_trailers_reach) if not ok then print("client:request() failed: ", err) os.exit(3) end sock:close()
还有两个问题:
- 如果有 query 参数,是直接按规范写在 path 中吗?类似:
local headers = { { name = ":method", value = "GET" }, { name = ":path", value = "/authorization?a=1&b=2&c=3" }, { name = ":scheme", value = "http" }, { name = "accept-encoding", value = "gzip" }, }
- 如何获取到返回值呢?如果靠回调函数的话,是否要设置一个全局变量去接收返回值?
Try to enable debug log in your nginx.conf and see whether there are some logs in your error.log.
For questions:
- Yep, the querystring should be written in
:path
- The status code is in the
:status
pseudo header
请问这个问题解决了吗?