Appearance
网络请求 AI 规范
本文档用于 AI 生成 HTTP 请求 Lua 代码。参数规则已按 HexLuaAPI_Net.hpp 校对。
GET
支持两种写法:
lua
local body, code, err = http_get("https://example.com", 5000)lua
local http = require("http")
local body, code, err = http.get("https://example.com", 5000)参数:
url:字符串。timeout:可选,毫秒,默认5000,范围1..60000。
返回值:
- 成功:
body, code - 失败:
nil, code, err
POST
lua
local body, code, err = http_post(
"https://example.com/api",
"a=1&b=2",
"application/x-www-form-urlencoded",
5000
)模块写法:
lua
local http = require("http")
local body, code, err = http.post(url, data, "application/json", 5000)参数:
url:字符串。data:字符串,请自行编码 JSON 或表单。ctype:可选,默认"application/x-www-form-urlencoded"。timeout:可选,毫秒,默认5000,范围1..60000。
JSON 请求模板
lua
local http = require("http")
local json = '{"name":"test"}'
local body, code, err = http.post(
"https://example.com/api",
json,
"application/json",
5000
)
if not body then
print("请求失败:", code, err)
return
end
print("状态码:", code)
print("响应:", body)AI 生成禁忌
- 不要把 Lua table 直接传给
http_post,必须先转字符串。 - 不要设置超过
60000的超时时间。 - 不要只判断
code == 200,先判断body ~= nil。 - 不要在高频循环里无间隔请求网络。