-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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() |