http result body

From Onset Developer Wiki
Revision as of 17:23, 24 January 2020 by BlueMountains (talk | contribs) (Created page with "{{Info|Function|Server|1.0.3}} {{FuncDescription|Returns the result body after a request was made.}} {{FuncSyntax|http_result_body(http)}} {{FuncParameters}} {{FuncParam|ht...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
http result body

Type: Function
Context: Server
Introduced: v1.0.3

Description

Returns the result body after a request was made.

Syntax

http_result_body(http)

Parameters

  • http
    The http identifier.

Return Value

  • Returns the result body as a string

Example

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

--[[
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

See also