url::get()
Perform HTTP GET request
url::get(url, request_params = "", headers = nothing, downloaded_to = "", progress_callback = nothing)
url
(string) — A URL stringrequest_params
(string|object, optional) — Params to be requested. The default value is ""headers
(object, optional) — HTTP headers. The default value is nothing
downloaded_to
(string, optional) — If downloaded_to
is set, then the response body will be saved to a file. The default value is ""progress_callback
(function_call, optional) — A function call to catch current download and upload progress. The default value is nothing
response
— Returns response
object
import url
' Simple usage example
response = url.get("https://example.com/")
if response.code == http.ok
writeln(response.body) ' Success
elseif response.code != 0
writeln("Error: HTTP " & response.code) ' HTTP error
else
writeln(response.error_string) ' Connection or other error
endif
' HTTP GET with headers and params
headers = {
"Authorization": "Basic QWxhZGRpbjpPcGVuU2VzYW1l",
"User-Agent": "myapp/1.0.0"
}
params = {
"id": "A0110",
"name": "Sarah Zaira"
}
response = url.get("https://faruq.id/misc/get.php", params, headers)
if response.code == 200
writeln(response.body)
elseif response.code != 0
writeln("Error: HTTP " & response.code)
else
writeln(response.error_string)
endif
' Simple example how to download a file
response = url.get("https://dinfio.org/releases/files/3.1.03/dinfio-3.1.03-macos.pkg", "", {}, "file.pkg")
if response.code == 200
writeln("Download complete!")
else
writeln(response.error_string)
endif
' Downloading a file with progress callback
response = url.get("https://dinfio.org/releases/files/3.1.03/dinfio-3.1.03-macos.pkg", "", {}, "file.pkg", progress())
if response.code == 200
writeln("Download complete!")
else
writeln(response.error_string)
endif
function progress(downloaded, total_download, uploaded, total_upload)
write("Downloading... " & bytes_unit(downloaded) & " of " & bytes_unit(total_download))
return 0 ' Note: 0 to continue downloading/uploading progress
' 1 to stop the progress
stop