Skip to content

Commit

Permalink
uv
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed May 14, 2024
1 parent 127fefa commit 0aef40c
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/lua/CATCHME/uv.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
local luv = vim.uv

-- Define the target server and endpoint
-- local host = "110.242.68.66"
local host = luv.getaddrinfo("www.baidu.com")[1].addr
print(vim.inspect(host))
local port = 80
local data = "{}" -- Form data to be sent

-- Create a TCP client
local client = luv.new_tcp()

-- Connect to the server
client:connect(host, port, function(err)
if err then
print("Error connecting to server:", err)
return
end

-- Send the HTTP POST request
client:write("POST \r\n" ..
"Host: " .. host .. "\r\n" ..
"Content-Length: " .. string.len(data) .. "\r\n" ..
"Content-Type: application/json\r\n" ..
"\r\n" ..
data)

-- Handle the response
client:read_start(function(err, chunk)
if err then
print("Error while reading:", err)
client:close()
return
end

if chunk then
-- Process or store the received data
print(chunk)
else
-- Response has been fully received
client:read_stop()
client:close()
end
end)
end)

-- Run the loop
luv.run()

0 comments on commit 0aef40c

Please sign in to comment.