Надсилання HTTP-запиту

Lua:
Надсилання HTTP-запиту

How to: / Як це зробити:

-- Ensure you have the 'lua-socket' library
local http = require("socket.http")
local ltn12 = require("ltn12")

-- Simple GET request
local response_body = {}
local res, code, response_headers = http.request{
    url = "http://httpbin.org/get",
    sink = ltn12.sink.table(response_body)
}

-- Check if the request succeeded
if code == 200 then
    print("Success:")
    print(table.concat(response_body))
else
    print("Failed:", code)
end

-- POST request with data
local response_body_post = {}
res, code = http.request{
    url = "http://httpbin.org/post",
    method = "POST",
    headers = {
        ["Content-Type"] = "application/x-www-form-urlencoded"
    },
    source = ltn12.source.string("key1=value1&key2=value2"),
    sink = ltn12.sink.table(response_body_post)
}

-- Check if the POST request succeeded
if code == 200 then
    print("POST Success:")
    print(table.concat(response_body_post))
else
    print("POST Failed:", code)
end

Sample output for GET request:

Success:
{
  "args": {}, 
  "headers": {
    "Host": "httpbin.org",
    ...
  },
  ...
}

Sample output for POST request:

POST Success:
{
  "form": {
    "key1": "value1",
    "key2": "value2"
  },
  ...
}

Deep Dive / Глибоке Занурення:

HTTP requests are the backbone of web communication, dating back to the 1990s. Choices for sending HTTP requests in Lua include libraries like lua-socket for simple interactions, and lua-http for more complex tasks. The lua-socket library’s http.request function handles the basic request-and-response cycle. It uses sockets to send and receive data, which can be manipulated with Lua streams (implemented by the ltn12 module). Both the request and the response can have headers to pass additional info (like content type). Error handling is essential; always check the status code (code) to make sure your request was successful.

See Also / Дивіться також: