Last active
January 26, 2020 00:28
-
-
Save BlueMountainsIO/b5bb4bcb4b6807b05346f465148ec5cf to your computer and use it in GitHub Desktop.
[Onset Server] HTTP request examples.
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
function test_get() | |
local r = http_create() | |
http_set_resolver_protocol(r, "any") | |
http_set_protocol(r, "https") | |
http_set_host(r, "postman-echo.com") | |
http_set_port(r, 443) | |
http_set_verifymode(r, "verify_peer") | |
http_set_target(r, "/get?foo=bar&onset=nice") | |
http_set_verb(r, "get") | |
http_set_timeout(r, 30000) | |
http_set_version(r, 11) | |
http_set_keepalive(r, false) | |
http_set_field(r, "user-agent", "Onset Server "..GetGameVersionString()) | |
if http_send(r, OnGetComplete, "Str L", 88.88, 1337) == false then | |
print("HTTP REQ NOT SENT :(") | |
http_destroy(r) | |
end | |
end | |
function OnGetComplete(a, b, c) | |
print("OnGetComplete", a, b, c) | |
end | |
function test_post() | |
local r = http_create() | |
http_set_resolver_protocol(r, "ipv4") | |
http_set_protocol(r, "http") | |
http_set_host(r, "ptsv2.com") | |
http_set_port(r, 80) | |
http_set_target(r, "/t/ozoab-1577627726/post") | |
http_set_verb(r, "post") | |
http_set_timeout(r, 30) | |
http_set_version(r, 11) | |
http_set_keepalive(r, true) | |
http_set_field(r, "user-agent", "Onset Server "..GetGameVersionString()) | |
local body = "" | |
body = body.."foo1="..url_encode("bar?") | |
body = body.."foo2="..url_encode("?d,_:%bar2?") | |
http_set_body(r, body) | |
http_set_field(r, "content-length", string.len(body)) | |
http_set_field(r, "content-type", "application/x-www-form-urlencoded; charset=utf-8") | |
if http_send(r, OnPostComplete, 1, 3.14, "OK s") == false then | |
print("HTTP REQ NOT SENT :(") | |
http_destroy(r) | |
end | |
end | |
function OnPostComplete(a, b, c) | |
print("OnPostComplete", a, b, c) | |
end | |
function test_error_code() | |
local r = http_create() | |
http_set_resolver_protocol(r, "ipv4") | |
http_set_protocol(r, "http") | |
http_set_host(r, "httpstat.us") | |
http_set_port(r, 80) | |
http_set_target(r, "/431") | |
http_set_verb(r, "get") | |
http_set_timeout(r, 30) | |
http_set_keepalive(r, false) | |
if not http_send(r) then | |
http_destroy(r) | |
end | |
end | |
AddEvent("OnPackageStart", function() | |
print(" http_client start") | |
--test_get() | |
--test_post() | |
--test_error_code() | |
print(" http_client end") | |
end) | |
--[[ | |
Generic Event that is called for every HTTP request. If you need to pass custom parameters | |
to it you can specify your own callback function in http_send including your parameters. | |
]]-- | |
AddEvent("OnHttpRequestComplete", function(http) | |
--[[if http_is_error(http) then | |
print("OnHttpRequestComplete failed for id", http..": "..http_result_error(http)) | |
else | |
print("OnHttpRequestComplete succeeded for id", http) | |
print_active_results(http) | |
end | |
http_destroy(http)]]-- | |
end) | |
function print_active_results(http) | |
local body = http_result_body(http) | |
local header = http_result_header(http) | |
local status = http_result_status(http) | |
print("\tBody: ", body) | |
print("\tHTTP Status: ", status) | |
print("\t Headers:") | |
for k, v in pairs(header) do | |
print("\t", k, v) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment