feat: http response as table

This commit is contained in:
MihailRis 2026-09-14 12:41:12 +03:00
parent 5f37118f74
commit ef3a5cf278
2 changed files with 42 additions and 10 deletions

View file

@ -127,8 +127,12 @@ network.get = function(url, callback, errorCallback, headers)
return request(url, { return request(url, {
method = "GET", method = "GET",
headers = headers, headers = headers,
on_response = callback, on_response = function(response)
on_error = errorCallback, callback(response.body)
end,
on_error = function(response)
errorCallback(response.status, response.body)
end,
follow_location = true, follow_location = true,
}) })
end end
@ -137,8 +141,12 @@ network.get_binary = function(url, callback, errorCallback, headers)
return request(url, { return request(url, {
method = "GET", method = "GET",
headers = headers, headers = headers,
on_response = callback and (function (response) return callback(Bytearray(response)) end), on_response = callback and (function (response)
on_error = errorCallback, return callback(Bytearray(response.body))
end),
on_error = function(response)
errorCallback(response.status, response.body)
end,
follow_location = true, follow_location = true,
}) })
end end
@ -150,8 +158,12 @@ network.post = function(url, body, callback, errorCallback, headers)
"Content-Type: application/json" "Content-Type: application/json"
}, headers), }, headers),
body = body, body = body,
on_response = callback, on_response = function(response)
on_error = errorCallback, callback(response.body)
end,
on_error = function(response)
errorCallback(response.status, response.body)
end,
}) })
end end

View file

@ -28,7 +28,7 @@ struct ResponseEventDto {
int status; int status;
bool binary; bool binary;
int requestId; int requestId;
std::vector<char> bytes; network::HttpResponse response;
}; };
enum NetworkDatagramSide { enum NetworkDatagramSide {
@ -122,7 +122,7 @@ static int l_request(lua::State* L, network::Network& network) {
response.status, response.status,
false, false,
currentRequestId, currentRequestId,
std::move(response.body)} std::move(response)}
)); ));
}; };
@ -493,11 +493,31 @@ static int l_pull_events(lua::State* L) {
lua::pushinteger(L, dto.requestId); lua::pushinteger(L, dto.requestId);
lua::rawseti(L, 3); lua::rawseti(L, 3);
lua::createtable(L, 0, 2);
lua::pushinteger(L, dto.response.status);
lua::setfield(L, "status");
if (dto.binary) { if (dto.binary) {
lua::create_bytearray(L, dto.bytes.data(), dto.bytes.size()); lua::create_bytearray(
L, dto.response.body.data(), dto.response.body.size()
);
} else { } else {
lua::pushlstring(L, std::string_view(dto.bytes.data(), dto.bytes.size())); lua::pushlstring(
L,
std::string_view(
dto.response.body.data(), dto.response.body.size()
)
);
} }
lua::setfield(L, "body");
lua::createtable(L, dto.response.headers.size(), 0);
for (int i = 0; i < dto.response.headers.size(); i++) {
lua::pushlstring(L, dto.response.headers[i]);
lua::rawseti(L, i + 1);
}
lua::setfield(L, "headers");
lua::rawseti(L, 4); lua::rawseti(L, 4);
break; break;
} }