http is error: Difference between revisions

From Onset Developer Wiki
No edit summary
No edit summary
 
Line 1: Line 1:
{{Info|Function|Server|1.0.3}}
{{Info|Function|Server|1.0.3}}


{{FuncDescription|After request was made, this function returns if an error occured. The error can be retrieved by [[http_result_error]].}}
{{FuncDescription|After request was made, this function returns if an error occured. The error can be retrieved by [[http_result_error]].
 
If you want to receive the HTTP status code, use [[http_result_status]].}}


{{FuncSyntax|http_is_error(http)}}
{{FuncSyntax|http_is_error(http)}}

Latest revision as of 17:24, 24 January 2020

http is error

Type: Function
Context: Server
Introduced: v1.0.3

Description

After request was made, this function returns if an error occured. The error can be retrieved by http_result_error.

If you want to receive the HTTP status code, use http_result_status.

Syntax

http_is_error(http)

Parameters

  • http
    The http identifier.

Return Value

  • Returns true if an error occurred.

Example

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

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